我遇到了简单的Java表单提交问题。
我有我的login.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The best login page in the world</title>
</head>
<body>
<h1>Connexion</h1>
<form id="asdasd" name="coucou" method="post" action="doLogin">
<input type="text" name="user_email" />
<input type="password" name="user_password" />
<input type="submit" name="asd" value="Se connecter" />
</form>
</body>
</html>
我的web.xml指定doLogin in fact the servlet loginServlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.courses.web.servlet.loginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/doLogin</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
我的loginServlet.java
public class loginServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse response) {
String username = req.getParameter("user_email");
String password = req.getParameter("user_password");
Console console = System.console();
console.printf("mail : %s\npassword: %s\n", username,password);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
没有什么比这更复杂,但是我从getParameter得到的变量总是为NULL,我不知道为什么。
我做错了什么?
答案 0 :(得分:0)
由于您尚未发布堆栈跟踪,因此唯一可以返回null
值的行就是这一行:
Console console = System.console();
你的评论注意到:我在Netbeans中运行glassfish 。所以,这一行将抛出NullPointerException
:
console.printf("mail : %s\npassword: %s\n", username,password); //console is null
尝试使用记录器来打印消息。只是为了一个简单的快速解决方案,请使用System.out.println
,直到您碰巧配置和使用记录器。所以,简而言之,替换这些行:
Console console = System.console();
console.printf("mail : %s\npassword: %s\n", username,password);
要:
System.out.printf("mail : %s\npassword: %s\n", username,password));