通过 Servlet 访问 JSP
Accessing a JSP via a Servlet
我正在学习 Java EE,我对 MVC 开发还很陌生,在此先感谢您的耐心等待。
我正在尝试编写一个可通过 servlet 访问的简单 JSP,但是当我尝试访问以下 URL 时出现 404 错误:
我不明白我哪里错了,或者我忘了写什么。
在这方面,我将不胜感激。
这是项目树结构(Eclipse):
这是我开发的文件。
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 -The 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 );
}
}
web.xml 中的 url 模式定义了用于访问应用程序的路径
您发布的应用程序 URL 确实连接了显示名称和模式。如果你喜欢,我认为你应该去申请
...localhost:8080/题词
有很多教程可以帮助我熟悉我应该使用 forward 还是 include 的基础。我检查了这个答案
http://www.tutorialspoint.com/servlets/servlets-first-example.htm
这里有一个拼写错误 inscription.jsp
public static final String VUE = "/WEB-INF/incription.jsp";
在你形成动作时你应该这样尝试:
action='/pro/inscription' OR action='/inscription'
我正在学习 Java EE,我对 MVC 开发还很陌生,在此先感谢您的耐心等待。
我正在尝试编写一个可通过 servlet 访问的简单 JSP,但是当我尝试访问以下 URL 时出现 404 错误:
我不明白我哪里错了,或者我忘了写什么。 在这方面,我将不胜感激。
这是项目树结构(Eclipse):
这是我开发的文件。
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 -The 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 );
}
}
web.xml 中的 url 模式定义了用于访问应用程序的路径
您发布的应用程序 URL 确实连接了显示名称和模式。如果你喜欢,我认为你应该去申请 ...localhost:8080/题词
有很多教程可以帮助我熟悉我应该使用 forward 还是 include 的基础。我检查了这个答案
http://www.tutorialspoint.com/servlets/servlets-first-example.htm
这里有一个拼写错误 inscription.jsp
public static final String VUE = "/WEB-INF/incription.jsp";
在你形成动作时你应该这样尝试:
action='/pro/inscription' OR action='/inscription'