我使用(jsp,servlet,apache tomcat 9,eclipse neon,oracle 11g)创建了一个servlet项目。我创建了index.jsp(获取登录详细信息并重定向到LoginServlet以获取数据库表中的用户名和密码验证。在index.jsp中,我甚至提到了一个“register.jsp”链接,如果是第一次用户则注册)。当我运行项目index.jsp页面打开时。
当我提供用户ID和密码详细信息(在数据库中手动输入用户名和密码详细信息)时,它成功地重定向了LoginServlet,然后重定向了servlet。
但是当我点击“注册”链接时,我收到上述错误( HTTP状态[404]?[未找到]类型状态报告消息/TodayServlet/register.jsp描述源服务器确实找不到目标资源的当前表示,或者不愿意透露它是否存在.Apache Tomcat / 9.0.0.M20 )
我的问题来自index.jsp,我无法打开“register.jsp”(“通过index.jsp中的锚标记提供的register.jsp)。
请查看我的目录结构和主要jsp和servlet以及web.xml代码的附件。
index.jsp(主要代码):
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<form action="LoginServlet" method="post">
enter name:<input type="text" name="name"><br>
enter pass:<input type="password" name="pass"><br>
<a href="register.jsp">New User Register here...!!!</a><br>
<input type="submit" value="login"><br>
</form>
</center>
</body>
</html>
register.jsp(主要代码):
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<form action="RegisterServlet" method="post">
enter name:<input type="text" name= "name1"> <br>
enter pass:<input type="text" name= "pass1"><br>
enter email:<input type="text" name="email1"><br>
<input type="submit" value="register"><br>
</form>
</center>
</body>
</html>
RegisterServlet.java(主要代码):
package com.pack;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String uname1=req.getParameter("name1");
String upass1=req.getParameter("pass1");
String uemail1=req.getParameter("email1");
req.setAttribute("username", uname1);
try{
Connection conn=DbConnection.getConnection();
PreparedStatement ps=conn.prepareStatement("insert into register_table values(?,?,?)");
ps.setString(1, uname1);
ps.setString(2, upass1);
ps.setString(3, uemail1);
int i=ps.executeUpdate();
if(i>0){
req.getRequestDispatcher("WelcomeServlet").forward(req, res);
}
else{
out.print("try registering again ");
req.getRequestDispatcher("register.jsp").include(req, res);
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
我对其他servlet(LoginServlet,WelcomeServlet ......)
没有任何问题LoginServlet.java(整个代码):
package com.pack;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
RequestDispatcher rd= null;
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String upassword=req.getParameter("pass");
out.println(upassword);
String uname2=req.getParameter("name");
out.println(uname2);
try{
Connection con=DbConnection.getConnection();
out.println("conection established");
PreparedStatement ps=con.prepareStatement("select * from register_table where username=? and password=?");
ps.setString(1, uname2);
ps.setString(2, upassword);
ResultSet rs= ps.executeQuery();
if(rs.next()){
out.println("username is "+rs.getString(1)+" and pwd is "+rs.getString(2));
req.setAttribute("uid", uname2);
req.getRequestDispatcher("MainServlet").forward(req, res);
}else{
out.println("user name or password incorrect.Check and login again or register");
req.getRequestDispatcher("index.jsp").include(req, res);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
web.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TodayServlet</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>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.pack.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>RegisterServlet</display-name>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.pack.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/RegisterServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>WelcomeServlet</display-name>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>com.pack.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/WelcomeServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>MainServlet</display-name>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.pack.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/MainServlet</url-pattern>
</servlet-mapping>
</web-app>
目录结构:
答案 0 :(得分:2)
我多次挣扎于这个问题。
我目前使用的解决方案是webapp(或保存视图的文件夹,如jsp)是否在部署程序集中。
试试这个:
右键单击项目> Build Path > Configure Build path > Deployment Assembly > Add(right hand side) > Folder >
(添加您的jsp文件夹,默认为src/main/webapp
)
答案 1 :(得分:1)
虽然需要保护Http访问的资源位于WEB-INF下。 通常,jsps用于向用户呈现的目的,这就是为什么它们被放置在WEB-INF之外。 但由于某些原因,如果你坚持将jsp文件保存在WEB-INF文件夹下而不是创建一个servlet, 并从注册链接调用该servlet。
在servlet中,您需要使用requestdispatcher重定向到register.jsp
保持您的Servlet代码如下:(我假设您的register.jsp直接在WEB-INF文件夹下提供)
@WebServlet("/ControlFlowServlet")
public class ControlFlowServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext()
.getRequestDispatcher("/WEB-INF/register.jsp");
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
在web.xml中添加此映射:
<servlet-mapping>
<servlet-name>ControlFlowServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
在你的index.jsp中保持这样:
<a href="${pageContext.request.contextPath}/register">New User Register here...!!!</a><br>
如需了解更多信息,请仔细阅读this解释。 如果您需要任何澄清,请告诉我。
答案 2 :(得分:0)
我遇到了这些问题,并通过添加新的精确来源解决了该问题: