Servlet在调用时不显示任何内容

时间:2014-07-30 04:10:40

标签: java jsp servlets

我只是在一段时间后为servlet / jsp编写代码。

当我调用servlet时,它给了我空白的浏览器,甚至没有错误。

我的Jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>

<body>      
        <form action="HelloServlet" method="POST">          
             Please enter a color <br>
            <input type="text" name="color"size="20px">
            <input type="submit" value="submit">                        
        </form>     
    </body> 

</html>

我的Servlet:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloServlet
 */
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("Servlet called...");
        String color = request.getParameter("color");
        PrintWriter out = response.getWriter();
        /*
         * out.println(
         * 
         * "<html> \n" + "<head> \n" +
         * "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \n"
         * + "<title> My first jsp  </title> \n" + "</head> \n" + "<body> \n" +
         * "<font size=\"12px\" color=\"" + color + "\">" + "Hello World" +
         * "</font> \n" + "</body> \n" + "</html>");
         */
        out.println("Aman.............");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

和web.xml:

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/HelloServlet</url-pattern>
  </servlet-mapping>
  <display-name>TestApp</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-list>
</web-app>

请帮帮我。

谢谢,

阿曼

3 个答案:

答案 0 :(得分:1)

将super添加到以下函数中,一切都会正常工作

protected void service(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {


      **super(request, response)   // add this**

}

service()方法是执行实际任务的主要方法。 servlet容器(即web服务器)调用service()方法来处理来自客户端(浏览器)的请求。

你已经覆盖了负责调用doget和post的函数。

希望这能解决您的疑问

答案 1 :(得分:0)

您犯的错误是您正在使用annotation以及通过部署描述符(web.xml)注册servlet。

如果使用注释,则不需要通过部署描述符注册servlet。但你应该有tomcat7,因为它不会在以前版本的tomcat中运行。 @WebServlet annotation用于映射具有指定名称的servlet。

此外,我假设您没有使用任何包,因为在url-mapping中你指定了一个。但是如果你使用一个名为mypackage的包并且你的servlet类在它里面,那么将url-mapping更改为类似的东西

<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>mypackage.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>

答案 2 :(得分:0)

受保护的服务方法检查请求的类型,如果请求类型为get,则调用doGet方法,如果请求类型为post,则调用doPost方法,依此类推

尝试删除此方法,并且您的servlet应该正常工作

protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

    }

或使用以下方法重写您的服务方法:

protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String method = request.getMethod();  
        if(method.equals("GET"))  
            doGet(request, response);  
        else
            doPost(request, response);
    }

您不会在服务方法中写任何内容,因此它永远不会调用doPost或doGet