0

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:

enter image description here

Obviously
  • 21
  • 2
  • 7

3 Answers3

2

I could see multiple errors in your post ,

  1. Replace the action attribute with ,

    <form action = "./login" method="post">

  2. If your using the version 2.5 , remove the import for annotation and also this line,

    @WebServlet("/login")

  3. Thirdly, to access the <form> elements in the servlet , use request#getParameter

    String username = request.getParameter("username"));

Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • When I do
    it redirects to localhost:8080/UserModule/WebContent/jsp/login which doesn't exist. My Servlet is in the same project, but under a different directory and package.
    – Obviously Nov 18 '14 at 12:52
  • @Obviously use a `./login` in the action attribute – Santhosh Nov 18 '14 at 12:54
  • I did set ./login in the action attribute like this: `
    `
    – Obviously Nov 18 '14 at 12:57
  • is this valid path `src.action.LoginServlet` of your servlet . post your directory structure in your question – Santhosh Nov 18 '14 at 12:58
  • I fixed this issue. It's still not working. Anyway, I posted my directory structure in my question. Thanks. – Obviously Nov 18 '14 at 13:04
  • @Obviously Can you add the pic here . restricted site for my network – Santhosh Nov 18 '14 at 13:11
  • I would, but it doesn't let me because I have less than 10 reputation. – Obviously Nov 18 '14 at 13:12
  • where have you placed your servlet in the project ? And what error you are getting now ? is that 404 ?> – Santhosh Nov 18 '14 at 13:14
  • UserModule is my project. I have a folder called src under it, with four packages under that named action, bo, dao, and vo. My servlet is inside action. I have another folder under UserModule called jsp, with my jsp file in it. WEB-INF is also under UserModule, and it has web.xml in it. `
    ` redirects to localhost:8080/login which is a 404. login is in src/action
    – Obviously Nov 18 '14 at 13:15
  • so it is enough to declare as `action.LoginServlet` in your `web.xml` and it will find the required servlet – Santhosh Nov 18 '14 at 13:17
0

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.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

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">
Oleh Novikov
  • 558
  • 5
  • 17
  • I know, but I am required to use version 2.5 – Obviously Nov 18 '14 at 12:50
  • Then `@WebServlet` will not work for you. Stick to the servlet mappings inside of your web.xml and also as said by @luiggi-mendoza consider replacig hardcoded localhost:8080 with ${request.contextPath} – Oleh Novikov Nov 18 '14 at 12:55
  • As I mentioned under @luiggi-Mendoza's post,
    only redirected to localhost:8080/login which doesn't exist
    – Obviously Nov 18 '14 at 12:56