I'm having a problem with a simple Java form submission.
I have my 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>
my web.xml specifiying that doLogin in infact 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>
And my 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";
}
}
Nothing really complicated but the variable that I get from getParameter are always NULL and I don't know why.
What am I doing wrong ?
Since you haven't posted a stacktrace, the only line that can have return a null
value is this one:
Console console = System.console();
Noted by your comment: I'm running glassfish in Netbeans . So, this line will throw a NullPointerException
:
console.printf("mail : %s\npassword: %s\n", username,password); //console is null
Try using a logger instead to print your messages. Just for a dirty quick solution, use System.out.println
instead until you happen to configure and use a logger. So, in short, replace these lines:
Console console = System.console();
console.printf("mail : %s\npassword: %s\n", username,password);
To:
System.out.printf("mail : %s\npassword: %s\n", username,password));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.