0

I am a beginner at JSP and Servlets. In my application folder structure is as follows:

Servlet Class: LearningJSP\src\servlet\UserDetailsServlet.

Jsp file : JSP\Inputform.jsp

The code is as follows:

package servlet;

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 UserDetails
 */

@WebServlet(urlPatterns = {"/userdetails"})

public class UserDetailsServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserDetailsServlet()
    {
        super();
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String name = request.getParameter("name");
        String surname = request.getParameter("surname");
        String age = request.getParameter("age");

        javabeans.UserDetailsValue userDetails = new javabeans.UserDetailsValue();
        userDetails.setAge(age);
        userDetails.setName(name);
        userDetails.setSurname(surname);

        request.setAttribute("message", "saved successfully.");
        request.getRequestDispatcher("/UserDetails.jsp").forward(request, response);
    }

}

InputForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Form</title>
</head>
<body>
<h1>Please fill up the data for Registration.</h1>
<form action = "/userdetails" method = "GET">
<input type = "text" name = "name"><br>
<input type = "text" name = "surname"><br>
<input type = "text" name = "age"><br>
<input type = "submit" value = "Register"><br>
</form>
</body>
</html>

I am getting

Error 404: Resource not found.

I think there is some mistake in Url in action tag in JSP, but after many trials I coundn't find my mistake.

Please help me to find error. Thanks in advance

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • what is it not finding? how did you configure your server/start page? – Stultuske Jan 26 '16 at 07:19
  • I have added JSP name in Web.xml file. When I am running the application on Tomcat server, JSP is getting displayed, after entering form values, when it should redirect to servlet, it gives me error 404 – SamkitJain Jan 26 '16 at 07:21
  • Try action="userdetails" without "/". – Panagiotis Stoupos Jan 26 '16 at 07:29
  • I tried with that. When I doing like that the URL patterns comes as follows... http://localhost:8081/LearningJSP/JSP/userdetails?name=data&surname=data&age=data @Panagiotis Stoupos – SamkitJain Jan 26 '16 at 07:35

3 Answers3

0

try using action = "../userdetails" .

Sindhoo Oad
  • 1,194
  • 2
  • 13
  • 29
  • I tried with that. When I doing like that the URL patterns comes as follows... http://localhost:8081/LearningJSP/JSP/userdetails?name=data&surname=data&age=data – SamkitJain Jan 26 '16 at 07:41
  • where have you put you jsp folder? can you paste your project structure? – Sindhoo Oad Jan 26 '16 at 07:43
  • structure is as follows: LearningJSP(project name)WebContent\JSP(folder name)\Inputform.jsp – SamkitJain Jan 26 '16 at 07:46
  • if you are you annotation then why you have mapped it again in web.xml? – Sindhoo Oad Jan 26 '16 at 07:48
  • Web.xml only contains the welcome-file name. I does not contain any servlet mapping information. You can check in below post, I have copied the code of web.xml (unformatted, though!) – SamkitJain Jan 26 '16 at 07:53
  • I have update ans, check it now, – Sindhoo Oad Jan 26 '16 at 08:01
  • Hi I tried with package name i.e. "servlet". I gave as follows action = "/servlet/userdetails".. still same result. I even tried complete path i.e. action = "LearningJSP/servlet/userdetails" – SamkitJain Jan 26 '16 at 08:11
0

Do you have the UserDetails.jsp page directly under the WebContent folder? If not, the Exception is comming from here:

request.getRequestDispatcher("/UserDetails.jsp").forward(request, response);

because the file does not exist at this path:

LearningJSP\WebContent\

One good exercise to see all the resources that are being called, would be to

  • open the browser
  • open up the console (F12)
  • go to Network
  • Click on Start capturing (CTRL + E in Chrome)
  • refresh the page to see all the resources requested by your page. One of them will have the status 404.

all the resources requested by one page

petre
  • 508
  • 7
  • 9
  • Actually my form is not getting submitted to Servlet at all. I checked it by writing a System.Out.println. Though I changed it to – SamkitJain Jan 26 '16 at 08:04
  • @Samkit, check my improved answer, it might help. You can see exactly which resource is not found. – petre Jan 26 '16 at 08:25
-3

Your Project Structure should be like this. Click this link

InputForm.jsp should have form tag as below:

UserDetailsServlet.java doGet method should have: request.getRequestDispatcher("/JSP/UserDetails.jsp").forward(request, response); Note: /JSP/UserDetails.jsp

This is my modified answer, please give me positive votes, if solution works.

abdulrafique
  • 304
  • 1
  • 11