Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trouble passing info from servlet to jsp

 
Ray Smilgius
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bill Siggelkow
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"/>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic