我使用(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(主要代码):
pageEncoding="ISO-8859-1"%>
Insert title hereenter name:
enter pass:
New User Register here...!!!
register.jsp(主要代码):
pageEncoding="ISO-8859-1"%>
Insert title hereenter name:
enter pass:
enter email:
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(代码):
TodayServlet
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
LoginServlet
LoginServlet
com.pack.LoginServlet
LoginServlet
/LoginServlet
RegisterServlet
RegisterServlet
com.pack.RegisterServlet
RegisterServlet
/RegisterServlet
WelcomeServlet
WelcomeServlet
com.pack.WelcomeServlet
WelcomeServlet
/WelcomeServlet
MainServlet
MainServlet
com.pack.MainServlet
MainServlet
/MainServlet
目录结构:
TodayServlet(项目名称)> src> com.pack> servlets
web-inf>(web.xml和jsp文件)