我正在尝试创建一个登录servlet。我有一个login.jsp,我使用表单获取数据,LoginServlet使用HttpSession,最后是我的web.xml 我在stackoverflow搜索了这里,我纠正了一些我发现的错误。即使找不到我的LoginServlet:
HTTP状态404 - /test_pages/main_pages/com.register/LoginServlet
type: Status report
message: /test_pages/main_pages/com.register/LoginServlet
description: The requested resource is not available.
我在状态404中看到我的servlet的路径是:/test_pages/main_pages/com.register/LoginServlet
这是错误的道路。我的servlet不在main_pages中。我在写作。我必须创建一个项目才能在apache tomcat中运行所有的东西,这让我很困惑。我的LoginServlet位于Java Resources / src / com.register中。 “com.register”是我的包裹。
你能告诉我一个正确的方式在我的表格中写下这个动作吗?你认为问题出在其他地方吗?请帮我!提前谢谢!
这是我的代码:
的login.jsp
.
.
.
<form action="com.register/LoginServlet" method="post">
<p>
<label id="upodeiksh">username</label>
<br />
<input type="text" name="username" id="koutaki" required/>
</p>
<br />
<p>
<label id="upodeiksh">password</label>
<br />
<input type="password" name="password" id="koutaki" required/>
</p>
<br />
<input type="submit" name="upload" value="login" id="submit_button"/>
</form>
LoginServlet.java
package com.register;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/jsp");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("test.jsp").include(request, response);
String name=request.getParameter("username");
String password=request.getParameter("password");
String psw = null;
Connection con = null;
PreparedStatement pr = null;
ResultSet rs = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ted","root","1234");
pr = con.prepareStatement("select * from ted.users where usrname=?;");
pr.setString(1, name);
rs = pr.executeQuery();
//An den brethei to onoma
if(rs == null)
{
out.print("Το όνομα χρήστη : "+name+" δεν υπάρχει");
request.getRequestDispatcher("main_pages/login.jsp").include(request, response);
}
else
{
//An brethei to onoma
while(rs.next())
{
psw = rs.getString("psw");
}
if(psw != password)
{
out.print("Λάθος κωδικός πρόσβασης");
request.getRequestDispatcher("main_pages/login.jsp").include(request, response);
}
else if(psw == password)
{
out.print("Γεια");
HttpSession session=request.getSession();
session.setAttribute("name",name);
}
}
}
catch (Exception e)
{
System.out.println("The error is=="+e.getMessage());
}
finally
{
try
{
con.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
的web.xml
.
.
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.register.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
.
.
答案 0 :(得分:0)
您的servlet映射是错误的。像这样更改您的表单操作
<form action="LoginServlet" method="post">