我遇到了一個簡單的Java表單提交問題。JSP中的Java post表單,get參數在servlet中返回NULL
我有我的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 specifiying是doLogin在INFACT在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,我不知道爲什麼。
我在做什麼錯?
哪個網址,你訪問,以顯示您的登錄看法? –
另外,既然您已經在使用JSF,爲何不將它用於您的登錄視圖?這樣你節省了太多的開發時間。順便說一下,如果您碰巧使用JSF 2,我建議您將Faces Servlet的URL模式更改爲「* .xhtml」或「* .jsp」,而不是舊的'/ faces/*'。 –
我從http:// localhost:8080/[projectname] /login.jsp訪問它。我對Java很陌生,如何使用JSF更改到我的登錄視圖? –