私はJSPとサーブレットで遊んでいて、次の問題があります:
小さな「ログイン機能」のようなものを実装する JSP があります。コードは次のとおりです。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
</head>
<body>
Enter username and password:<br />
<hr />
<form method="GET" name="login" action="LoginCheck">
username<br />
<input type="text" name="loginName" /><br />
Password<br />
<input type="password" name="password" /><br />
<input type="submit" value="login" />
<input type="reset" value="reset" />
</form>
</body>
</html>
ご覧のとおり、サーブレット LoginCheck にアクセスしたいと考えています。コードは次のとおりです。
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginCheck extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String loginName = request.getParameter("loginName");
String password = request.getParameter("password");
if(loginName.equals("admin"))
{
if(password.equals("test"))
{
out.println("Username and password are correct!");
}else
{
out.println("Password were are incorrect!<br />");
out.println("<a href='index.jsp'>Here</a> you can get back to the Loginpage!");
}
}else
{
out.println("Username doesn't exist.<br />");
out.println("<a href='index.jsp'>Here</a> you can get back to the Loginpage!");
}
}
}
別の JSP でサーブレットのコードを使用すると、すべて正常に動作しますが、サーブレットにアクセスできません。XML は次のとおりです。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" 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>Servlet</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>
</web-app>
サーバーは Jetty サーバーであり、いいえ、Tomcat を使用することは許可されていません。Eclipse での私のプロジェクトの構造は次のようになります: http://imageshack.us/photo/my-images/204/eclipseo.png/
サーブレットにアクセスできない理由を教えてください。
ありがとう!