0

I am trying to send a string from a Java servlet to JSP but I always get a null in the string

Test.java servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String s = "HelloWolrd";
    System.out.println(s);
    response.setContentType("text/jsp");
    request.setAttribute("s", s);
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/test.jsp");
    dispatcher.forward(request,response);
}

test.jsp

<body><%= request.getAttribute("s")%> </body>

web.xml has servlet class mapped to apis. Test and url-pattern as /test.

My Directory structure

Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
Kavya Vishwanath B
  • 107
  • 1
  • 1
  • 10

2 Answers2

0

The test.jsp file is placed outside the WEB-INF as per your project structure. Validate it again.

Servlet:

public class Test extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String s = "HelloWolrd";
        System.out.println(s);
        response.setContentType("text/jsp");
        request.setAttribute("s", s);
        RequestDispatcher dispatcher = getServletContext()
                .getRequestDispatcher("/test.jsp");       // CHANGE IT HERE
        dispatcher.forward(request, response);
   }
}

web.xml:

<servlet>
    <servlet-name>XYZ/servlet-name>
    <servlet-class>apis.Test</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>XYZ</servlet-name>
    <url-pattern>/test</url-pattern>
</servlet-mapping>

Have a look at my another post How does a servlets filter identify next destination is another filter or a servlet/jsp? for detailed description on Servlet Matching Procedure used for url-pattern.

Note: Always try to avoid Scriplet instead use JavaServer Pages Standard Tag Library and Expression Language.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • nope that dint make any difference and the previous values <% = request.getAttribute("s") are also null. :( – Kavya Vishwanath B Jun 01 '14 at 16:06
  • can you explain the flow of the application and share the `web.xml` as well for quicker solution. – Braj Jun 01 '14 at 16:09
  • I am a newbie and I am just trying to send a string value from servlet to jsp page once the page is requested . User requests the page localhost:8080/Traders/test.jsp and the string value s needs to be displayed thats yet – Kavya Vishwanath B Jun 01 '14 at 16:12
  • Is jsp calling properly from Servlet? Try with dummy data first in jsp. – Braj Jun 01 '14 at 16:13
  • I am not the downvoter but I think it has nothing to do with form. Though it may be an issue I dont see form being submitted anywhere. OP is trying to access an attribute in JSP set in the servlet. – Aniket Thakur Jun 01 '14 at 16:23
  • Yes I have edited my post but that is the another issue that will be encountered once this issue is resolved. In JPS `apis/Test` is used in form as `action` and url-pattern `/test` is defiend in `web.xml`. – Braj Jun 01 '14 at 16:24
  • The `test.jsp` file is placed outside the `WEB-INF` as per your project structure. Look at the my edited post again. – Braj Jun 01 '14 at 16:38
  • i edited my code as per ur suggestion by changing /WEB-INF/test.jsp to /test.jsp but dint work . I have tried all the suggestions I still get a null value :( please help me out with this . Thankx – Kavya Vishwanath B Jun 01 '14 at 17:07
  • Simply share the files and step to execute the flow. I have already asked you how the flow is designed. What URL is passed in the browser. – Braj Jun 01 '14 at 17:11
  • my web.xml http://pastebin.com/JQ8gZB1K , Test.java http://pastebin.com/rgRLb3VB , http://pastebin.com/nm6LDedq test.jsp – Kavya Vishwanath B Jun 01 '14 at 17:16
  • `web.xml` looks fine. Have you tried with just hello message in the jsp file. – Braj Jun 01 '14 at 17:17
  • By hello world u mean just to echo hello world on screen ? Yes i did and it works:( – Kavya Vishwanath B Jun 01 '14 at 17:26
-1

Also as per your eclipse directory structure you have your JSP under WebContent folder. So you need to do

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/traders/test.jsp");

Few other thing you can try

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/traders/test.jsp?s=s");

and then in your JSP you can do

<%= request.getAttribute("s")%>

OR

you can use Session

HttpSession session = request.getSession(true);
session.setAttribute("s", "s");

and do

<%= session.getAttribute( "s" ) %>
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289