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 JSP and the fly likes Passing Document from servlet to JSP see code 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 » Java » JSP
Bookmark "Passing Document from servlet to JSP see code" Watch "Passing Document from servlet to JSP see code" New topic
Author

Passing Document from servlet to JSP see code

Ray Smilgius
Ranch Hand

Joined: Jan 29, 2001
Posts: 120
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 of asp page.

url = new URL ("http://ssteward:5000");
// 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.
if(user == "" | | password == "" | | user.equals("") | | password.equals(""))
{
user = "NO INPUT";
password = "NO INPUT";
String content = "<?xml version=\"1.0\"?><employees><employee><user>" + user + "</user><password>" + password + "</password></employee></employees>";
System.out.println(content);
System.out.println("inside if");
printout.writeBytes (content);
res.sendRedirect("http://" + req.getHeader("host") + "/" + "star/template.jsp");
printout.flush ();
printout.close ();

}
else
{
String content = "<?xml version=\"1.0\"?><employees><employee><user>" + user + "</user><password>" + password + "</password></employee></employees>";
out.println(content);
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);

System.out.println("my doc "+ doc.toString());


// in.close();

NodeList idNl=doc.getElementsByTagName("user");

NodeList nameNl=doc.getElementsByTagName("password");



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.println(idname+"=>"+idvalue+" "+name+"=>"+nvalue+"<BR>");

}




// close the Input Stream

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());
}

}
//////////////////////////////////////////////////////////
I need to pass the Document doc to a jsp page called template.jsp ::: see the jsp page code below

////////////////////////////////////
<%@page import="javax.xml.parsers.DocumentBuilderFactory,java.net.*,java.io.*,javax.xml.parsers.DocumentBuilder,org.w3c.dom.*" %>
<%!Document myDom;%>
<html>
<head>
<title>My first template</title>
</head>
<body>

<%
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse("http://rsmilgius:8080/star/employee.xml");
///////how do I get the doc from the servlet and go 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.println(idname+"=>"+idvalue+" "+name+"=>"+nvalue+"<BR>");

}
%>

</body>
</html>
I need to parse the the xml string from the server in the servlet then pass the Document doc from my servlet to the jsp page template.jsp
Any help greatly appreciated
Thanks Ray Smilgius

SCJO, SCJD, SCWCD, I-Net+, A+, Network+, MCSD, MCDBA, MCP, MCT
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
I suspect that the problem is that you are using sendRedirect - this essentially drops everything that has been accomplished and
causes the browser to do a new, separate request. You should
be using request dispatcher to send the modified request to the JSP.
Bill
Ray Smilgius
Ranch Hand

Joined: Jan 29, 2001
Posts: 120
Ok Thanks Bill,
I will try this......
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Document from servlet to JSP see code
 
Similar Threads
Java Web Services 2
Can I load a xml string versus reading a file
Can some one help I keep getting a d**m null value and no examples work
trouble passing info from servlet to jsp
need to get response xml string back from servlet to jsp page