我使用(jsp、servlet、apache 9、eclipse、oracle11g)创建了一个servlet项目。我已经创建了index.jsp (获取登录详细信息并重定向到LoginServlet以获得数据库表中的用户名和密码验证)。在index.jsp中,我甚至提到了一个"register.jsp“链接,如果第一次用户注册)。当我运行项目时,会打开index.jsp页面。
当我提供用户id和密码详细信息(在数据库中手动输入用户名和密码详细信息)时,它将成功地重定向LoginServlet等servlet。
但是当我点击“注册”的链接时,我会得到上面提到的错误(HTTP状态404?找不到类型状态报告消息/TodayServlet/Regier.jsp描述,源服务器没有找到目标资源的当前表示形式,也不愿意透露其中的存在。Apache Tomcat/9.0.0.M20)
我的问题来自index.jsp,我无法打开"register.jsp“("register.jsp通过index.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"> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <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"> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <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); 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); 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>