0

I'm trying to write a servlet using generated HTML code, rather then printing out static HTMLs.

I'm using Eclipse-EE Europa and Tomcat 6.

I tried to use the flowing tips from HERE

But instead of printing the desired attribute It seems that the jsp ignoring the attribute or the attribute is empty.

Here is the servlet:

package com.serv.pac;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * Servlet implementation class for Servlet: testServlet
 *
 */
 public class testServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public testServlet() {
        super();
    }       

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String message = "doGet response";

        request.setAttribute("message", message);
        request.getRequestDispatcher("/testServlet/WEB-INF/index1.jsp").forward(request, response);
        PrintWriter out = response.getWriter();
        out.println("the servlet");
    }   

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("first servlet");
    }               
}

Here is the JSP

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2370960</title>
    </head>
    <body>
         <p>Message: ${message}</p>
    </body>
</html>

And the following is the :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>servlet1_test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>

    <welcome-file>index1.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>testServlet</display-name>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>com.serv.pac.testServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/testServlet</url-pattern>
  </servlet-mapping>
</web-app>

And that is what I'm getting in the browser:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2370960</title>
    </head>
    <body>
         <p>Message: </p>
    </body>
</html>

As one may see there is nothing after the "Message" in the html body, as if the message attribute is empty.

Thank You

Community
  • 1
  • 1
bindkeeper
  • 76
  • 1
  • 7

1 Answers1

0

The only way I can see this happenning is you aren't actually accessing your Servlet.

You've declared a <welcome-file>

<welcome-file>index1.jsp</welcome-file>

So if you try to hit

localhost:8080/YourContextPath

the default Servlet will render and send you that jsp with a missing message attribute.

If you want to hit your actual Servlet, you need to use

localhost:8080/YourContextPath/testServlet

Note that you need to change

request.getRequestDispatcher("/testServlet/WEB-INF/index1.jsp").forward(request, response);

to

request.getRequestDispatcher("/WEB-INF/index1.jsp").forward(request, response);

and move your file under WEB-INF.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724