简体   繁体   中英

Eclipse 404 Requested resource not available

I've been struggling with this issue for a few hours and I can't seem to figure out what's wrong. Did a lot of googling and digging through stuff but nothing seems to work. I'm developing a web application using eclipse and i get the following error when accessing a resource:

HTTP Status 404 - /EditForm
type Status report

message /EditForm

description The requested resource is not available.

A bit of background information: I'm using a JSP to submit a form that's meant to use a servlet written in java in order to process the information. It's called editProfile.jsp and here is the part where the form is being submitted with information.

  <form CLASS="form-horizontal" METHOD=POST ACTION="/EditForm">

    <div class="form-group">
            <label class="control-label col-sm-2"><b>New Address</b></label>
            <INPUT TYPE=TEXT PLACEHOLDER="Type new address here." NAME=address SIZE=20><BR>
    </div>
    <div class="submit_btn col-sm-2"><INPUT TYPE=SUBMIT></div> 

  <form>

The Servlet is as follows:

 package user;

import java.io.IOException;
import java.sql.*;

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


@WebServlet("/EditForm")
public class EditForm extends HttpServlet{


/**
 * 
 */
private static final long serialVersionUID = 102831973239L;
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {

    HttpSession session = request.getSession();
    String currForm =(String) request.getParameter("currForm");
    String user = (String) session.getAttribute("username");



    if(currForm.equals("address")) {

        String sqlStmt = "UPDATE db.User SET address = '"+ currForm+"' WHERE username = '"+user+"';";
        doSQL(sqlStmt);
        response.sendRedirect("profile.jsp");

    }
    else if(currForm.equals("phone")) {

        String sqlStmt = "UPDATE db.User SET phone = '"+ currForm+"' WHERE username = '"+user+"';";
        doSQL(sqlStmt);
        response.sendRedirect("profile.jsp");

    }

    else if(currForm.equals("email")) {

        String sqlStmt = "UPDATE db.User SET email = '"+ currForm+"' WHERE username = '"+user+"';";
        doSQL(sqlStmt);
        response.sendRedirect("profile.jsp");

    }

}

public void doSQL(String stmt) {

    try {
        DatabaseOperation op = new DatabaseOperation();
        op.connect();
        op.executeQuery(stmt);

    } catch(Exception e) { e.printStackTrace(); }   

}

}

And my Web.xml inside my WEB-INF folder is :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" 
 version="3.1">
 <display-name>Website</display-name>
 <welcome-file-list>
     <welcome-file>index.html</welcome-file>
     <welcome-file>index.htm</welcome-file>
     <welcome-file>index.jsp</welcome-file>
     <welcome-file>default.html</welcome-file>
     <welcome-file>default.htm</welcome-file>
     <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>

<servlet>
  <servlet-name>EditForm</servlet-name>
  <servlet-class>user.EditForm</servlet-class>  
</servlet>

</web-app>

My app directory structure:

   --Website
        |--Deployment Descriptor: Website
        |--JAX-WS Web Services
        |--Java Resources
        |      |--src
        |      |   |--user(package)
        |      |         |--EditForm.java
        |      |
        |      |--Libraries   
        |
        |--JavaScript Resources
        |--Referenced Libraries
        |--build
        |--WebContent
        |      |--META-INF
               |--WEB-INF
               |    |--lib
               |    |--web.xml
               |
               |--editProfile.jsp

I would be really grateful if someone can point me in the right direction and tell me what I'm doing wrong exactly that my resource can't be accessed. Thank you.

A Servlet Container can host multiple web apps, so the URI you're mapping, "/EditForm", is only going to match requests relative to your web app's context path when it is deployed on the server, not to "/EditForm" absolutely. pageContext.getServletContext().getContextPath() can give you that path if you want to prepend it to the form action value with a JSP Expression (assuming you'd rather not hard-code it).

You also don't need to declare the Servlet within the deployment descriptor if you're using the javax.servlet.annotation.WebServlet annotation, at least, not unless you're going to do something else with it in the deployment descriptor as well.

I think you are not properly setting the path of you servlet. instead of action=/EditForm try going for action=../EditForm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM