通过Servlet访问JSP

时间:2015-04-14 20:41:53

标签: jsp java-ee servlets model-view-controller

我正在学习Java EE,而且我对MVC开发还很陌生,请提前感谢您的耐心等待。

我正在尝试编写一个可通过servlet访问的简单JSP,但是当我尝试访问以下URL时出现404错误:

  

http://localhost:8080/pro/inscription

     

http://localhost:8080/inscription

我不知道我哪里出错了,或者我忘了写什么。 在这方面,我将不胜感激。

这是项目树结构(Eclipse):

enter image description here

以下是我开发的文件。

  

inscription.jsp

<%@ page pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Inscription</title>
<link type="text/css" rel="stylesheet" href="/inc/styles/css/form.css" />
</head>
<body>
    <form method="get" action="inscription">
        <fieldset>
            <legend>Inscription</legend>
            <p>Vous pouvez vous inscrire via ce formulaire.</p>

            <label for="email">Adresse email <span class="requis">*</span></label>
            <input type="text" id="email" name="email" value="" size="20"
                maxlength="60" /> <br /> <label for="motdepasse">Mot de
                passe <span class="requis">*</span>
            </label> <input type="password" id="motdepasse" name="motdepasse" value=""
                size="20" maxlength="20" /> <br /> <label for="confirmation">Confirmation
                du mot de passe <span class="requis">*</span>
            </label> <input type="password" id="confirmation" name="confirmation"
                value="" size="20" maxlength="20" /> <br /> <label for="nom">Nom
                d'utilisateur</label> <input type="text" id="nom" name="nom" value=""
                size="20" maxlength="20" /> <br /> <input type="submit"
                value="Inscription" class="sansLabel" /> <br />
        </fieldset>
    </form>
</body>
</html>
  

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http:/'2Fwww.w3.mrg/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">
  <display-name>pro</display-name>
  <servlet>
    <servlet-name>Inscription</servlet-name>
    <servlet-class>servlets.Inscription</servlet-class>

    <init-param>
        <param-name>Auteur</param-name>
        <param-value>Imad</param-value>
    </init-param>   

  </servlet>

  <servlet-mapping>
    <servlet-name>Inscription</servlet-name>
    <url-pattern>/inscription</url-pattern>
  </servlet-mapping>
</web-app>
  

Inscription.java - servlet

package servlets;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Inscription extends HttpServlet
{
    /**
     * UID
     */
    private static final long   serialVersionUID    = 7413041593835021978L;
    /**
     * Path de la vue
     */
    public static final String  VUE                 = "/WEB-INF/incription.jsp";

    @Override
    protected void doGet( HttpServletRequest req , HttpServletResponse resp ) throws ServletException , IOException
    {
        this.getServletContext().getRequestDispatcher( VUE ).forward( req , resp );
    }
}

2 个答案:

答案 0 :(得分:1)

web.xml中的url-pattern定义了用于访问应用程序的路径

您发布的应用程序URL确实连接了displayname和模式。如果您喜欢,我认为您应该使用您的应用程序 ...本地主机:8080 /碑文

有很多教程可以熟悉基础,我应该使用前进或包含。我检查了这个答案

http://www.tutorialspoint.com/servlets/servlets-first-example.htm

答案 1 :(得分:1)

以下是inscription.jsp

的拼写错误
public static final String  VUE = "/WEB-INF/incription.jsp";

在你的表格中你应该尝试这样做:

action='/pro/inscription' OR action='/inscription'