0

I have the following form in jsp which contains a text field and a button

<form action="/login.do" method="Post">
Enter 
<br/><br/>
Your Name <input type="text" name="name"/>
<br/><br/>
Password <input type="text" name="password"/>
<br/><br/>
<select name="Gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<br/><br/>
<input type="submit" value="Login"/>

My Servlet:

@WebServlet(urlPatterns = "/login.do")
public class LoginServlet extends HttpServlet{
protected void doGet(HttpServletRequest request, javax.servlet.http.HttpServletResponse response) 
        throws ServletException, IOException{
    request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);


}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String name =request.getParameter("name");
    String password = request.getParameter("password");
    String gender = ((String)request.getParameter("Gender")=="Female")? "Ms.":"Mr.";

    if(new ValidateUser().validate(name, password)){
    request.setAttribute("name", name);
    request.setAttribute("password", password);
    request.setAttribute("gender", gender);
    request.getRequestDispatcher("/WEB-INF/view/welcome.jsp").forward(request, response);;
    } else {
    request.setAttribute("errorMes", "Login Failed");
    request.getRequestDispatcher("/WEB-INF/view/login.jsp").forward(request, response);;    
    }
}

According to a tutorial when I click to button in the jsp page, it will trigger the doPost() method in the Servlet class.

However I keep getting the HTTP Status 404 - /login.do Error

Please Help! ...

My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- webapp/WEB-INF/web.xml -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

  <display-name>To do List</display-name>
  <welcome-file-list>
    <welcome-file>login.do</welcome-file>
  </welcome-file-list>

When I run the project, I am connecting to this URL: http://localhost:8081/in28Minutes-first-webapp/

when I click to button I was connected to this url: http://localhost:8081/login.do and it gives the "HTTP Status 404 - /login.do" error

I have tried "${pageContext.request.contextPath}/login.do", it still gives the error

I have this pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.in28minutes</groupId>
    <artifactId>in28Minutes-first-webapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <source>1.7</source>
                    <target>1.7</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                    <contextReloadable>true</contextReloadable>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

EricHo
  • 183
  • 10
  • 1
    what is the complete url you have used in the browser ? – Vasu Oct 29 '16 at 09:00
  • 1
    Please read http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – JB Nizet Oct 29 '16 at 09:04
  • You need to prepend the context path of the webapp: `action="/myfirstwebapp/login.do"` for example. To avoid hard-coding it, use `"${pageContext.request.contextPath}/login.do"`, or the `` JSTL tag. – JB Nizet Oct 29 '16 at 09:08
  • When I run the project, I am connecting to this URL: http://localhost:8081/in28Minutes-first-webapp/ when I click to button I was connected to this url: http://localhost:8081/login.do and it gives the "HTTP Status 404 - /login.do" error I have tried "${pageContext.request.contextPath}/login.do", it still gives the error – EricHo Oct 29 '16 at 13:11

1 Answers1

0

My suggestion is:

You should in action parameter of form tag set location that you to be on page localhost:8081/in28Minutes-first-webapp/login.do instead of localhost:8081/login.do. Try set one of following settings: action="login.do", action="localhost:8081/in28Minutes-first-webapp/login.do" or action="/in28Minutes-first-webapp/login.do".

I have tried "${pageContext.request.contextPath}/login.do", it still gives the error

How's your given error? How's a value of the ${pageContext.request.contextPath}?