0

Below I have uploaded Servlet ,JSP and .xml file. I have done some research and made changes by looking at previous questions posted in this site related to my problem.But error still appears

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form id="form1" name="form1" method="post" action="/selectcar">
   <p align="center">PLEASE ENTER YOUR NAME AND SELECT DESIRED CAR</p>
   <p align="center"> NAME                -
     <label for="nameText"></label>
     <input type="text" name="nameText" id="nameText" />
   </p>
   <p align="center"> CAR   - 
     <label for="selectCar"></label>
     <select name="selectCar" id="selectCar">
       <option>BMW</option>
       <option>Benz</option>
       <option>Audi</option>
       <option>Ducati</option>
     </select>
   </p>
   <p align="center">
     <input type="submit" name="submit" id="submit" value="Submit" />
   </p>
</form> 
    </body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
      http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <servlet>
    <servlet-name>car</servlet-name>
    <servlet-class>com.car.select.CarServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>car</servlet-name>
    <url-pattern>/selectcar</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

CarServlet.java

package com.car.select;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class CarServlet extends HttpServlet {


    @Override
   public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{

    response.setContentType("text/html");

    String name=request.getParameter("nameText");
    String car= request.getParameter("selectCar");

    PrintWriter out= response.getWriter();
    out.println(name + "   " +car);


   }

   }

When clicking submit after entering data it goes to localhost:8080/selectcar page and displays 404 not found .I think I am making some mistake in xml version but not able to pin point it.I use glass fish server 4.0 .. Thanks for your help

pratt
  • 47
  • 1
  • 7

4 Answers4

0

Put your jsp page at right place that means in WEB-INF folder structure should be like

src
    -your servlet files
webapp
    -WEB-INF
        -your jsp pages
bluish
  • 26,356
  • 27
  • 122
  • 180
Ramesh
  • 15
  • 8
  • My folder structure Myapplication -->web pages -->web-inf --->has jsp and xml....... My application --->source packages --->com.car.select-->servelt – pratt Dec 04 '14 at 05:56
0

Are you testing with a "POST" method? you have only implemented doPost on your servlet code.

see this previous answer : doGet and doPost in Servlets

Community
  • 1
  • 1
0

Put ur CarServlet class in right place as mentioned in web.XML.
The folders should be inside web-inf/classes.
See the link for reference: http://www.studytonight.com/servlet/steps-to-create-servlet-using-tomcat-server.php

0

There are two ways here:

  1. If you are putting your jsp files in WEB-INF folder, you cannot access it like http://localhost:8080/app_name/index.jsp. In your web.xml, edit lines to include "WEB-INF" before index.jsp

    /WEB-INF/index.jsp

    Now, you can access index.jsp directly with url http://localhost:8080/app_name and for accessing servlet use full url http://localhost:8080/app_name/selectcar in tag

  2. If jsp files are located directly at WebContent folder (outside WEB-INF folder), do not make any changes to web.xml. Edit index.jsp files, remove "/" before "selectcar" in form action as,

    and it should work

vijay
  • 494
  • 5
  • 17