Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 请求getParameter(<;fieldName>;)返回null_Java_Servlets_Tomcat8 - Fatal编程技术网

Java 请求getParameter(<;fieldName>;)返回null

Java 请求getParameter(<;fieldName>;)返回null,java,servlets,tomcat8,Java,Servlets,Tomcat8,我已经使用html创建了一个登录页面,其中我获取用户名和密码并显示用户名。我创建了一个用于显示用户名的servlet,其中我使用request.getParameter()来显示用户名。我正在使用eclipseide和Tomcat部署应用程序。我无法执行此操作,因为该方法返回null。代码如下: PageLogin.java /** * Servlet implementation class PageLogin */ @WebServlet("/login") public cla

我已经使用
html
创建了一个登录页面,其中我获取用户名和密码并显示用户名。我创建了一个用于显示用户名的
servlet
,其中我使用
request.getParameter()
来显示用户名。我正在使用
eclipseide
Tomcat
部署应用程序。我无法执行此操作,因为该方法返回null。代码如下:

PageLogin.java

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

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

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        processRequest(request, response);
    }

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

    }

    public void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String user = request.getParameter("username");
        String pass = request.getParameter("password");
        PrintWriter out = response.getWriter();

        System.out.println("username : "+user);
        System.out.println("password : "+pass);

        String htmlResponse = "<html>";
        htmlResponse += "<h2>Username : " + user + "</h2>";
        htmlResponse +="</html>";

        out.println(htmlResponse);
    }

}
/**
*Servlet实现类PageLogin
*/
@WebServlet(“/login”)
公共类PageLogin扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
/**
*@参见HttpServlet#HttpServlet()
*/
公共页面登录(){
超级();
//TODO自动生成的构造函数存根
}
/**
*@参见HttpServlet#doGet(HttpServletRequest请求,HttpServletResponse响应)
*/
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
//TODO自动生成的方法存根
//response.getWriter().append(“服务于:”).append(request.getContextPath());
processRequest(请求、响应);
}
/**
*@请参阅HttpServlet#doPost(HttpServletRequest请求,HttpServletResponse响应)
*/
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
//TODO自动生成的方法存根
processRequest(请求、响应);
}
public void processRequest(HttpServletRequest请求、HttpServletResponse响应)引发IOException{
字符串user=request.getParameter(“用户名”);
String pass=request.getParameter(“密码”);
PrintWriter out=response.getWriter();
System.out.println(“用户名:“+用户”);
System.out.println(“密码:“+pass”);
字符串htmlResponse=“”;
htmlResponse+=“用户名:”+user+”;
htmlResponse+=“”;
out.println(htmlResponse);
}
}
index.html

    <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>

<form action="PageLogin" method="post" >
    Username :<input type="text" name="username"><br>
    Password :<input type="password" name="password"><br>

    <input type="submit" value="Login">

</form>

</body>
</html>
    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>LoginQ3</display-name>

  <servlet>
  <servlet-name>loginpage</servlet-name>
  <servlet-class>PageLogin</servlet-class>
  </servlet>

  <servlet-mapping>
  <servlet-name>loginpage</servlet-name>
  <url-pattern>/display</url-pattern>
  </servlet-mapping>

  <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>

登录
用户名:
密码:
web.xml

    <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>

<form action="PageLogin" method="post" >
    Username :<input type="text" name="username"><br>
    Password :<input type="password" name="password"><br>

    <input type="submit" value="Login">

</form>

</body>
</html>
    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>LoginQ3</display-name>

  <servlet>
  <servlet-name>loginpage</servlet-name>
  <servlet-class>PageLogin</servlet-class>
  </servlet>

  <servlet-mapping>
  <servlet-name>loginpage</servlet-name>
  <url-pattern>/display</url-pattern>
  </servlet-mapping>

  <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>

登录Q3
登录页
页面登录
登录页
/展示
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
当我使用url时

我得到的输出是:


用户名:null

你是说
发布
还是
获取
?我对这一点不熟悉,所以我只想在使用url时显示用户名given@PritomMazumdar如果您访问
http://localhost:8080/LoginQ3/login?username=fred&password=secret
您可能会更成功。这复制了JSP中的“登录”按钮应该执行的操作……这取决于您试图实现的操作。如果您想提交表单,然后将其显示给用户,这很好(尽管您应该从
doGet
中删除
processRequest(request,response);
)。如果您想显示您在先前提交的请求中使用的用户名,则需要首先将其存储在某个位置。就像在一个会议上。
    <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>

<form action="PageLogin" method="post" >
    Username :<input type="text" name="username"><br>
    Password :<input type="password" name="password"><br>

    <input type="submit" value="Login">

</form>

</body>
</html>
    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>LoginQ3</display-name>

  <servlet>
  <servlet-name>loginpage</servlet-name>
  <servlet-class>PageLogin</servlet-class>
  </servlet>

  <servlet-mapping>
  <servlet-name>loginpage</servlet-name>
  <url-pattern>/display</url-pattern>
  </servlet-mapping>

  <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>