This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes send variable value  from one servlet to another servlet  for itext pdf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "send variable value  from one servlet to another servlet  for itext pdf" Watch "send variable value  from one servlet to another servlet  for itext pdf" New topic
Author

send variable value from one servlet to another servlet for itext pdf

archana tiwari
Ranch Hand

Joined: Mar 15, 2012
Posts: 44
I have three servlet i have to pass textfield value in one servlet(pdf.java) to two servlet(Database.java and Converthtml3.java) code is given below.

pdf.java

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class pdf extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>pdf conversion </title>");
out.println("</head>");
out.println("<body>");
out.println("<p><br><br><center><b>");
out.println("<html ><head>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />");
out.println("<style type=\"text/css\">");
out.println("</style>");
out.println("</head>");
out.println("<body>");
out.println("</b><br><form action=\"Database\" method=\"GET\" name=\"upform\">");
out.println("<table width=\"1269\" border=\"1\">");
out.println("<tr>");
out.println(" <td colspan=\"5\">");
out.println("Type:");
out.println("<select name=\"select\">");
out.println("<option>Local</option>");
out.println("<option>l2</option>");
out.println("</select>");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td width=\"270\">");
out.println("<input type=\"text\" name=\"textfield\" />");
out.println("</td>");
out.println("<td width=\"242\">");
out.println("Sec ");
out.println("<input type=\"text\" name=\"textfield2\" />");
out.println("</td>");
out.println("</td>");
out.println("</tr>");
out.println("</td>");
out.println("<td colspan=\"4\">");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td colspan=\"5\">");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td colspan=\"5\">");
out.println("<option> 1 </option>");
out.println("</select>");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td colspan=\"5\">");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td colspan=\"5\">");
out.println(" NO.:");
out.println("<input type=\"text\" name=\"textfield9\" />");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>");
out.println("</td>");
out.println("<td colspan=\"4\">");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>");
out.println("</td>");
out.println("<td colspan=\"4\">");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("</td>");
out.println(" </tr>");
out.println("<tr>");
out.println(" <td>1.</td>");
out.println("<td>");
out.println(" <input type=\"text\" name=\"textfield12\" />");
out.println("</td>");
out.println(" <td>");
out.println(" <input type=\"text\" name=\"textfield15\" />");
out.println(" </td>");
out.println(" <td>");
out.println(" </td>");
out.println(" <td>");
out.println(" <input type=\"text\" name=\"textfield18\" />");
out.println(" </td>");
out.println(" </tr>");
out.println(" <tr>");
out.println(" <td>2.</td>");
out.println(" <td>");
out.println(" </td>");
out.println(" <td>");
out.println(" <input type=\"text\" name=\"textfield16\" />");
out.println(" </td>");
out.println(" <td>");
out.println(" </td>");
out.println(" <td>");
out.println(" <input type=\"text\" name=\"textfield19\" />");
out.println(" </td>");
out.println(" </tr><tr>");
out.println(" <td>3.</td>");
out.println(" <td>");
out.println(" <input type=\"text\" name=\"textfield14\" />");
out.println(" </td>");
out.println(" <td>");
out.println(" <input type=\"text\" name=\"textfield17\" />");
out.println(" </td>");
out.println(" <td>");
out.println(" <select name=\"select11\">");
out.println(" </select>");
out.println("</td>");
out.println("<td>");
out.println("<input type=\"text\" name=\"textfield20\" />");
out.println("</td>");
out.println("</tr>");
out.println("</table></td>");
out.println("</tr>");
out.println("</table>");
out.println("<br>");
out.println("<input type=\"submit\" name=\"Submit\" value=\"Submit\" >");
out.println("</form>");
out.println("</body>");
out.println("</html>");

}
}

Database.java

package LoginExample;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Database extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Connection con = null;
Statement stmt = null;
//ResultSet rs = null;
String userName = "";
String password = "";
String url = "";
res.setContentType("text/html");
String a = req.getParameter("textfield9");
String b = req.getParameter("textfield2");
PrintWriter out = res.getWriter();
try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
con= DriverManager.getConnection (url, userName, password);
stmt = con.createStatement();
//PreparedStatement pst = con.prepareStatement("INSERT into I VALUES(?,?,?,?,?,?, to_date(?,'YYYY/MM/DD'),?,?)");
PreparedStatement pst = con.prepareStatement("INSERT into I1 VALUES(?,?)");
pst.setString(1,a);
pst.setString(2,b);
//pst.setString(3,c);
// pst.setString(4,d);
//pst.setString(5,e);
// pst.setString(6,f);
// pst.setString(7,g);
// pst.setString(8,h);
//pst.setString(9,i);
int numRowsChanged = pst.executeUpdate();
if(numRowsChanged!=0){
out.println("<br>Record has been inserted");

}
else{
out.println("failed to insert the data");
}
pst.close();
out.println("<HTML><HEAD><TITLE></TITLE></HEAD>");
out.println("<BODY>");
out.println("<form method=\"GET\" action=\"/LoginExample/Converthtml3\">");
out.println("<p>");
out.println("<input type=\"submit\" value=\"generate pdf\" name=\"B1\">");
out.println("</p>");
out.println("</form>");
out.println("</BODY></HTML>");
}
catch(ClassNotFoundException e1) {
out.println("Couldn't load database driver: " + e1.getMessage());
}
catch(SQLException e1) {
out.println("SQLException caught: " + e1.getMessage());
}
finally {
try {
if (con != null) con.close();
}
catch (SQLException ignored) { }
}
}
}


Converthtml3.java


package LoginExample;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.itextpdf.text.Font;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Paragraph;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.Phrase;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.sql.*;


public class Converthtml3 extends HttpServlet
{
public Connection con = null;
public Statement stmt =null;
public ResultSet rs =null ;
String s1,s2;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException
{
Document document=new Document();

try
{
res.setContentType("application/pdf");
PdfWriter.getInstance(document,res.getOutputStream());
document.open();
//String n ="345" ;
String n = req.getParameter("textfield9");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("", "", "");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from I1 where NO='" + n+ "' ");
while(rs.next())
{
s1=rs.getString("NO");
s2=rs.getString("SEC");
}
Paragraph p1=new Paragraph("fgj \n"+"jfj\n"+"dfhh \n"+"hdfgh");
p1.setAlignment(Element.ALIGN_CENTER);
document.add(p1);
Paragraph p2=new Paragraph("dfs \n"+"fdsg \n");
p2.setAlignment(Element.ALIGN_RIGHT);
document.add(p2);
Paragraph p3=new Paragraph("dfh \n"+"fgh \n");
p3.setAlignment(Element.ALIGN_LEFT);
document.add(p3);
Paragraph p4=new Paragraph("hdfh \n"+"bvnvc \n");
p4.setAlignment(Element.ALIGN_RIGHT);
document.add(p4);
Paragraph p5=new Paragraph("FORM");
p5.setAlignment(Element.ALIGN_CENTER);
document.add(p5);
Paragraph p6=new Paragraph("fjf");
p6.setAlignment(Element.ALIGN_LEFT);
document.add(p6);
Paragraph p7=new Paragraph("hjfj \n");
p7.setAlignment(Element.ALIGN_RIGHT);
document.add(p7);
Paragraph p8=new Paragraph("jdfj \n" );
p8.setAlignment(Element.ALIGN_LEFT);
document.add(p8);
Paragraph p9=new Paragraph("DECLARATION ");
p9.setAlignment(Element.ALIGN_CENTER);
document.add(p9);
Paragraph p10= new Paragraph("jgfj \n");
p10.setAlignment(Element.ALIGN_MIDDLE);
document.add(p10);
Paragraph p11=new Paragraph("ghj \n");
p11.setAlignment(Element.ALIGN_RIGHT);
p11.setSpacingAfter(30);
document.add(p11);
PdfPTable table = new PdfPTable(4);
PdfPCell cell = new PdfPCell(new Phrase("No"));
PdfPCell cell1 = new PdfPCell(new Phrase("Desc"));
PdfPCell cell2 = new PdfPCell(new Phrase("Quay"));
PdfPCell cell3= new PdfPCell(new Phrase(" Cost"));
table.addCell(cell);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(" ");
table.addCell(" ");
table.addCell(" ");
table.addCell(" ");
table.addCell(" ");
table.addCell(" ");
table.addCell(" ");
table.addCell(" ");
table.addCell(" ");
table.addCell(" ");
table.addCell(" ");
table.addCell(" ");
table.setSpacingAfter(30);
document.add(table);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
Paragraph p12=new Paragraph("hfj \n");
p12.setAlignment(Element.ALIGN_RIGHT);
document.add(p12);
Paragraph p13=new Paragraph("ghdh \n\n");
p13.setAlignment(Element.ALIGN_LEFT);
document.add(p13);
Paragraph p14=new Paragraph("hfgh \n");
p14.setAlignment(Element.ALIGN_LEFT);
document.add(p14);
}
catch(ClassNotFoundException e1) {
System.out.println("Couldn't load database driver: " + e1.getMessage());
}
catch(SQLException e1) {
System.out.println("SQLException caught: " + e1.getMessage());
}
catch(DocumentException de)
{
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
document.close();
}
}





Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
please use code tags.
your problem is not clear..
and if you want to use textfield value into both of your servlets, you can just do it using request.getParameter.

archana tiwari
Ranch Hand

Joined: Mar 15, 2012
Posts: 44
in pdf.java, in form action field i have given only Database.java servlet name so the value through string c= getParameter("textfield") is redirected to database.java but using string c=getParameter("textfield") in Converthtml3.java value is not redirected to Converthtml3.java , null value is printd when using this..
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2

null value is printd when using this..


this will be because you are not passing the value of your textfield to your Converthtml3.java..
you need to pass the value of textfield to your Converthtml3.java.
you can pass textfield value using querystring with your form action to Converthtml3.java from Database.java...

hope this helps....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: send variable value from one servlet to another servlet for itext pdf
 
Similar Threads
Itext with java : PDF nested table issues
web dynpro application
web dynpro application coding
FieldPositioningEvents in itext fails on multiple pages
PdfPTable