2016-12-10 1 views
-1

Моя веб-программа позволит пользователю вводить имя и фамилию, а после того, как пользователь нажмет кнопку отправки, он напечатает имя и фамилия.Java EE - HTTP Error 404 - ресурс недоступен - после нажатия кнопки «Отправить» на странице

Получаю ошибку HTTP 404 после того, как пользователь нажал кнопку «Отправить».

enter image description here

Ошибка

enter image description here

Это моя структура файла

enter image description here

Мои коды

PrintFormContent.java

package form; 

import java.io.IOException; 
import java.io.PrintWriter; 

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

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

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

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

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
    } 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     String fname = request.getParameter("name"); 
     String lname = request.getParameter("lname"); 

     try{ 
       response.setContentType("text/html"); 
       PrintWriter out = response.getWriter(); 
       out.print("<p>First Name</p>" + fname); 
       out.print("<p>Last Name</p>" + lname); 

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

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Form Test</title> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" 
    href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
<script 
    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
</head> 
<body> 
    <form id="PrintFormContent" name="PrintFormContent" role="form" 
     action="/testForm/form/PrintFormContent" method="POST"> 
     <div class="form-group row"> 
      <label for="fname" class="col-md-2">First Name</label> 
      <div class="col-md-4"> 
       <input type="text" class="form-control" id="fname" 
        placeholder="Enter First Name" name="fname" /> 
      </div> 
     </div> 
     <div class="form-group row"> 
      <label for="lname" class="col-md-2">First Name</label> 
      <div class="col-md-4"> 
       <input type="text" class="form-control" id="lname" 
        placeholder="Enter Last Name" name="lname" /> 
      </div> 
     </div> 
     <div style="text-align: center;"> 
      <button id="btn" type="submit" class="btn btn-default">Submit</button> 
     </div> 
    </form> 
</body> 
</html> 

web.xml

<?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>testForm</display-name> 
    <servlet> 
    <servlet-name>PrintFormContent</servlet-name> 
    <servlet-class>PrintFormContent</servlet-class> 
    </servlet> 

    <welcome-file-list> 
    <welcome-file>/testForm/program/form/index.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

Пожалуйста, помогите.

ответ

0

Положите вашу страницу приветствия непосредственно под WebContent (так что путь будет /index.jsp), а затем установить в вашем web.xml:

<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
0

вы объявили свой сервлет PrintFormContent но вы посылаете запрос на /testForm/form/PrintFormContent. Вам необходимо изменить его в декларации формы:

<form id="PrintFormContent" name="PrintFormContent" role="form" 
    action="/PrintFormContent" method="POST"> 

кстати. не забудьте изменить имя параметра в сервлет fname:

String fname = request.getParameter("fname"); 

попробовать добавить это web.xml, как-то ваш, кажется, не эту заметку работать

<servlet-mapping> 
    <servlet-name>PrintFormContent</servlet-name> 
    <url-pattern>/PrintFormContent</url-pattern> 
</servlet-mapping> 

проверить это, может быть, у вас есть та же проблема: webservlet-annotation-doesnt-work-with-tomcat-8

+0

не работает. Я даже попытался перейти на @WebServlet («/ testForm/form/PrintFormContent») также не работает – user3820292

+0

hmm, можете ли вы попытаться добавить 'System.out.println (« test »);' в 'doGet() ', а затем вызвать' localhost: 8080/PrintFormContent' в вашем браузере и проверить журнал, если вы видите «тест» – JohnnyAW

+0

Я вижу ошибку 404. – user3820292

 Смежные вопросы

  • Нет связанных вопросов^_^