0

When i run my Mavenproject in Eclipse on the index.jsp it opens it. Than i am trying to open my AdressServlet trough /EnterAddress here i get the error.

Index.jsp:

<html>
<body>
<h2>Welcome</h2>
<p> 
We are going to get started with some question. 
First we will need some information about you.
</p>
<a href="/EnterAddress">Start</a>
</body>
</html>

AddressServlet:

package Servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import Bean.SurveyBean;
import Service.SurveyService;
@WebServlet(value = "/EnterAddress", initParams = {
          @WebInitParam(name = "addressPage",
                value = "/WEB-INF/pages/Address.jsp"),
          @WebInitParam(name = "QuestionURL", value = "Question") })
public class AddressServlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void init() throws ServletException{
        super.init();
        }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SurveyBean bean = new SurveyBean();
           HttpSession sess = req.getSession();
           sess.setAttribute("surveyBean", bean);
           resp.sendRedirect("/pages/Address.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession sess = req.getSession();
           SurveyBean bean = (SurveyBean) sess.getAttribute("surveyBean");
           bean.setName(req.getParameter("name"));
           bean.setStreet(req.getParameter("street"));
           bean.setNumber(req.getParameter("number"));
           bean.setZipcode(req.getParameter("zipcode"));
           bean.setCity(req.getParameter("city"));
           bean.setEmail(req.getParameter("email"));
           sess.setAttribute("surveyBean", bean);
    }





}

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

Address.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>Insert title here</title>
</head>
<body>

<form method="POST">
Name: <input type="text" name="name"/>
Street: <input type="text" name="street"/>
Number: <input type="text" name="number"/>
Zipcode: <input type="text" name="zipcode"/>
email: <input type="text" name="email"/>
<input type="submit" value="OK"/>
</form>

</body>
</html>

Image of stucture of jsp files

I have no idea why i get the error. I don't get a error on index.jsp. But when i press Start on de jsp i get the error

When i do the things edited by BalusC It still doesn't work i have no idee what i am doing wrong or how i can fix it

Alex K
  • 22,315
  • 19
  • 108
  • 236
Dylan Gomes
  • 93
  • 11

1 Answers1

3

First, Here are a couple of things to keep in mind:
Any jsp files under WEB-INF cannot be directly accessed by typing something like localhost:8080/WEB-INF/foo.jsp on the browser.
The servlet can access them.

Here are something you need to do

  • Replace your web.xml Doctype tag with the following:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    

    The above change makes sure that annotation will work for your servlet. This may not be a direct problem because I do not know if you can access the servlet or not, returning 404 can be caused by not finding the jsp as well.

  • Using response.sendRedirect() is the same as typing the jsp url on the browser which will not get the resource because the resource is under WEB-INF. Also the path WEB-INF is not even included in your url.
    You will need to forward to the jsp in your servlet like so:

    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/pages/Address.jsp");
    dispatcher.forward(request, response);  
    
Minjun Yu
  • 3,497
  • 4
  • 24
  • 39
  • I know you cant access it by just typing it but its when i press the link, or when i run the address jsp in my eclipse. When i replace the web.xml i cant even run my index.jsp than when i place the normal back i can still run it. – Dylan Gomes May 25 '16 at 07:32
  • Adding "/WEB-INF/something/" before "myown.jsp" worked for me too. – zookastos Jul 25 '17 at 06:03