JSP页面在Servlet程序中不起作用

时间:2013-10-16 16:37:03

标签: java jsp servlets post servlet-listeners

实际上我正在尝试使用servlet显示从JSP表单获取的详细信息。但是我无法显示JSP页面。但我可以看到程序在Servlet中进入POST方法。

这是我的代码,

Startup.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="controlServlets" method="post">
        <input type="text" name="name"/><br>        
        <input type="text" name="group"/>
        <input type="text" name="pass"/>
        <input type="submit" value="submit">            
    </form>

</body>
</html>

的web.xml

<web-app>

  <servlet>
    <servlet-name>controlServlets</servlet-name>
    <servlet-class>com.selenium8x8.servlet.ControlServlets</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>controlServlets</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

</web-app>

ControlServlets.java

import java.io.IOException;

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

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


    public ControlServlets() {
        super();
        // TODO Auto-generated constructor stub
    }


//    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);  
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            String name = request.getParameter("name");
            String group = request.getParameter("group");
            String pass = request.getParameter("pass");
            System.out.println("Name :"+ name);
            System.out.println("group :"+ group);
            System.out.println("pass :"+ pass);
            System.out.println("Post method");
    }

}

在控制台中,

我可以看到以下内容,

Name :null
group :null
pass :null
Post method

请帮助......

1 个答案:

答案 0 :(得分:1)

第一部分)如果您想为您的应用程序使用web.xml,那么您需要进行以下更改:

1)在Startup.jsp中将action标记的<form>属性更改为

<form action="ControlServlets" method="post"> 
              ↑

2)在web.xml中将<servlet-mapping>更改为

<servlet-mapping>
 <servlet-name>controlServlets</servlet-name>
 <url-pattern>/ControlServlets</url-pattern> 
</servlet-mapping>  

3)ControlServlets.javaweb.xml中提到的<servlet-class>com.selenium8x8.servlet.ControlServlets</servlet-class> ↑ 进行了多次更改

ControlServlets.java

这是包名称,因此您必须在package com.selenium8x8.servlet; //in your code it is missing

中有第一个声明
//import javax.servlet.annotation.WebServlet;

然后,请注意以下两行

//@WebServlet("/ControlServlets")

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

@WebServlet("/ControlServlets")
public class ControlServlets extends HttpServlet {   
  ...
  .....
  .......
} 

现在,运行应用程序,它将为您提供所需的输出。


第二部分)如果你想使用@WebServlet注释,就像你做的那样

web.xml

然后,不需要<servlet> <servlet-name>controlServlets</servlet-name> <servlet-class>com.selenium8x8.servlet.ControlServlets</servlet-class> </servlet> <servlet-mapping> <servlet-name>controlServlets</servlet-name> <url-pattern>/ControlServlets</url-pattern> </servlet-mapping> 。以上内容基本相同:

@WebServlet

要使用Java EE 6 / Servlet 3.0注释,您需要{{1}}