Servlet to JSP
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Abdul_Ambalam
Posted On:   Friday, November 30, 2001 08:38 PM

Here I am sending a String object from a servlet to JSP. I am getting NullPointerException when I run this servlet in tomcat server. I try to get String object using request.getAttribute("message"); in JSP but I am getting null value. How to solve this problem. The following is the servlet code. import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class JSPTest extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String message = "This is a test"; req.setAttribute("message", message); Pr   More>>

Here I am sending a String object from a servlet to JSP. I am getting NullPointerException when I run this servlet in tomcat server. I try to get String object using request.getAttribute("message"); in JSP but I am getting null value. How to solve this problem. The following is the servlet code.

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;


public class JSPTest extends HttpServlet

{



public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException

{

String message = "This is a test";

req.setAttribute("message", message);

PrintWriter out = res.getWriter();

res.setContentType("text/html");

out.println(" Hi ");

RequestDispatcher rd = getServletContext().getRequestDispatcher("http://localhost:8080/test/SerJsp.jsp");


rd.forward(req,res);


}

}

   <<Less

Re: Servlet to JSP

Posted By:   Bozidar_Dangubic  
Posted On:   Sunday, December 2, 2001 08:48 AM

you are writing the response in

PrintWriter out = res.getWriter();
res.setContentType("text/html");

then you are trying to forward to a JSP,
but you cannot do that. once get the writer to a response object and write content to it, you no longer can forward. it certainly is a strange error message that you are getting, but remove the code I copied above (writing to an output before forwarding) because that is not going to work. I am not even sure what were you trying to accomplish with those two lines of code, writing the response and then forwarding the response to a JSP to write more response.... can't be done!

Re: Servlet to JSP

Posted By:   Norval_Hope  
Posted On:   Saturday, December 1, 2001 09:14 PM

The servlet looks fine, can you please include the code for your JSP (my guess is perhaps the scope is wrong on the jsp:useBean directive?)
About | Sitemap | Contact