Reputation: 21
I am trying to send data from a JSP file to a Java Servlet. I've seen so many examples on here on how to do it, and I believe I am doing it the right way, but for some reason the doPost() methods is not being called inside my servlet.
Here is what I added to my web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>src.action.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
Here is my JSP file
<html>
<head>
</head>
<body>
<form action = "localhost:8080/UserModule/src/action/login" method="post">
Username<input type = "text" name = "username"><br><br>
Password<input type = "password" name = "password"><br><br>
<input type = "submit" value = "Submit">
</form>
</body>
</html>
Here is my Java servlet:
package action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String username = (request.getAttribute("username")).toString();
System.out.println("asd");
String password = (String) request.getAttribute("password");
System.out.println(username);
}
}
It's giving me an error on import javax.servlet.annotation.WebServlet;
and on @WebServlet("/login")
It's because they were only implemented in the spec v3.0, while I am only allowed to use spec v2.5 (required to at work). Anyone has any idea how to solve this issue?
My directory:
Upvotes: 0
Views: 5089
Reputation: 85789
Your class is in package action
so its full name is action.LoginServlet
. You have configured src.action.LoginServlet
as the servlet class. Remove src
from there.
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>action.LoginServlet</servlet-class>
</servlet>
This part:
<form action = "localhost:8080/UserModule/src/action/login" method="post">
You have lot of boilerplate code there. Instead, use Expression Language to add localhost:8080/webappName
like this:
<form action = "${request.contextPath}/login" method="post">
Since you're using Servlet Specification 2.5, you cannot use @WebServlet
annotation. So remove it from your Serlvet class. Otherwise, start using Servlet 3.0 or superior by changing the servlet specification in your web.xml
To retrieve parameters from the submitted <form>
use ServletRequest#getParameter
. request#getAttribute
is used in JSP to retrieve attributes to display the data when using Expression Language.
Upvotes: 0
Reputation: 8207
I could see multiple errors in your post ,
Replace the action attribute with ,
<form action = "./login" method="post">
If your using the version 2.5 , remove the import for annotation and also this line,
@WebServlet("/login")
Thirdly, to access the <form>
elements in the servlet , use request#getParameter
String username = request.getParameter("username"));
Upvotes: 2
Reputation: 578
Just remove @WebServlet("/login")
This annotation and servlet mapping are interchangeable. So when you use one of these another definition is not required.
In order to use @WebServlet
webapp's web.xml has to be declared as version 3.0
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
Upvotes: 0