0

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

这是我的代码,

启动.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

请帮忙...

4

1 回答 1

1

第 I 部分)如果您想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.java的几个变化中web.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")

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


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

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 {   
  ...
  .....
  .......
} 

那么,就不需要了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

于 2013-10-18T06:50:35.170 回答