aspose file tools*
The moose likes JSP and the fly likes trouble passing info from servlet to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "trouble passing info from servlet to jsp" Watch "trouble passing info from servlet to jsp" New topic
Author

trouble passing info from servlet to jsp

Ray Smilgius
Ranch Hand

Joined: Jan 29, 2001
Posts: 120
Please see the code and comments below for my issue and problem
///////////////////////////////////////////////////////////////////////////////template.jsp/////////////////////////////////////////////////////////////////////////////////
<%@page import="javax.xml.parsers.DocumentBuilderFactory,java.net.*,java.io.*,javax.xml.parsers.DocumentBuilder,org.w3c.dom.*" %>
<%!Document myDom;%> is this correct ??? if I call it the myDom
<html>
<head>
<title>My first template</title>
</head>
<body>

<%
<--comment out --DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); //dont need
<--comment out --DocumentBuilder db=dbf.newDocumentBuilder(); //dont need
<--comment out --Document doc=db.parse("http://rsmilgius/FortKnox/employee.xml"); //dont need
//how do I retrieve the Document from the servlet and declare it to doc so I can continue from here ???
NodeList idNl=doc.getElementsByTagName("id");
NodeList nameNl=doc.getElementsByTagName("name");
%>
The Data Response from IDS<p/>
<%
for(int i=0;i<nameNl.getLength();i++)
{
String idname=idNl.item(i).getNodeName();
String idvalue=idNl.item(i).getFirstChild().getNodeValue();
String name=nameNl.item(i).getNodeName();
String nvalue=nameNl.item(i).getFirstChild().getNodeValue();

out.print("Name of insured person (" + nvalue + ")" + "<br>");
}
%>

</body>
</html>

/////the servlet code /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class pleasework extends HttpServlet {

URL url;
URLConnection urlConn;
DataOutputStream printout;
BufferedReader input;



public void init() throws ServletException {

System.out.println("TestServlet : init");
}
public void destroy() {
System.out.println("TestServlet : destroy");
}


public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
try
{

String user = req.getParameter("username");
String password = req.getParameter("password");

PrintWriter out = res.getWriter();




url = new URL ("http://ssteward:5001");
// URL connection channel.
urlConn = url.openConnection();
// Let the run-time system (RTS) know that we want input.
urlConn.setDoInput (true);
// Let the RTS know that we want to do output.
urlConn.setDoOutput (true);
// No caching, we want the real thing.
urlConn.setUseCaches (false);
// Specify the content type.
urlConn.setRequestProperty("Content-Type", "application/InScope");

// Send POST output.
printout = new DataOutputStream (urlConn.getOutputStream ());

//send the content to the server.

String content = "<?xml version=\"1.0\"?><employees><employee><user>" + user + "</user><password>" + password + "</password></employee></employees>";
System.out.println(content);
//send contents to remote server
printout.writeBytes (content);
printout.flush ();
printout.close ();


// Get response data and put it into a DOM and parse it....


InputStream in = urlConn.getInputStream();

DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();

DocumentBuilder db=dbf.newDocumentBuilder();

Document doc=db.parse(in);

///////////////////////this is where I am baffled I need to attach the Document doc and send it on to template.jsp

//setAttribute(Dom,doc); ???

//this line of code works find below and template.jsp is displayed but need to attach Document
// and foward it to template.jsp where I can process it. Also, I need to also know how to retreive the doc in
//the template.jsp and then I am on my way....

getServletContext().getRequestDispatcher("/template.jsp").forward(req,res);



// in.close();

}
catch (IOException ioe)
{
System.out.println("11");
System.err.println(ioe.getMessage());
}

catch (SAXException e)
{
System.err.println(e.getMessage());
System.out.println(e.getMessage());
}

catch (ParserConfigurationException ess)
{
System.err.println(ess.getMessage());
System.out.println(ess.getMessage());
}


}



}
Thanks in advance
ray smilgius.

------------------
Sun Certified Java Programmer
Sun Certified Java Developer
I-Net Certified
A+ Certified
Network+ Certified
MCP


SCJO, SCJD, SCWCD, I-Net+, A+, Network+, MCSD, MCDBA, MCP, MCT
Bill Siggelkow
Ranch Hand

Joined: Jun 27, 2001
Posts: 57
In your servlet, do the following
req.setAttribute("Dom", dom);
Then in your JSP you could use scriptlet like
<% Dom dom = (Dom) request.getAttribute("dom")%>
or you could use the use bean tag as follows
<jsp:useBean id="dom" class="Dom" scope="request"/>


Bill Siggelkow
Jade Cove Solutions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble passing info from servlet to jsp
 
Similar Threads
How to pass a parsed XML Document object from servlet to JSP??
need to get response xml string back from servlet to jsp page
Passing Document from servlet to JSP see code
Can I load a xml string versus reading a file
Java Web Services 2