我正在尝试使用生成的 HTML 代码编写一个 servlet,而不是打印出静态 HTML。
我正在使用 Eclipse-EE Europa 和 Tomcat 6。
我尝试使用来自HERE的流动提示
但是没有打印想要的属性,似乎jsp忽略了属性或者属性为空。
这是小服务程序:
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");
    }               
}
这是JSP
<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2370960</title>
    </head>
    <body>
         <p>Message: ${message}</p>
    </body>
</html>
以下是:
<?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>
这就是我在浏览器中得到的:
<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2370960</title>
    </head>
    <body>
         <p>Message: </p>
    </body>
</html>
可以看到,在 html 正文中的“Message”之后没有任何内容,就好像 message 属性为空一样。
谢谢你