• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing Document from servlet to JSP see code

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Thanks Bill,
I will try this......
 
reply
    Bookmark Topic Watch Topic
  • New Topic