我尝试创建一个简单的登录表单,它使用用户名和密码,检查mysql数据库表。请原谅我这个java的东西初学者......如果匹配它重定向到主页。但我无法执行它。有人帮我解决这个问题。谢谢快速回复。我得到的tomcat错误是请求的资源(/ UserDemo / firstserv)不可用。我知道还有更多的错误。请问我在这里发帖....帮助我......
SerExam.java
package myPack;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SerExam extends HttpServlet
{
Connection con;
PreparedStatement ps;
ResultSet rs;
public void init(ServletConfig config)throws ServletException
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","tiger");
}
catch (ClassNotFoundException e)
{
System.out.println(e);
}
catch (SQLException e)
{
System.out.println(e);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request, response);
{
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
String username=request.getParameter("username");
String password=request.getParameter("password");
pw.println("<html><body>");
try
{
ps=con.prepareStatement("select * from loginvalidation where username=? and password=?");
ps.setString(1, username);
ps.setString(2, password);
rs=ps.executeQuery();
if(rs.next())
{
pw.println("<h3>welcome " +" " + username +"</h3>");
RequestDispatcher rd1=request.getRequestDispatcher("./home.html");
rd1.include(request,response);
//or
//response.sendRedirect("./home.html");
pw.println("<form method=\"post\" action=\"Login.html\">");
pw.println("<input type=\"submit\" name=\"logout\" " + "value=\"Logout\">");
pw.println("</form>");
}
else
{
pw.println("<center><h3>invalid username/password Enter Correct username/password</h3></center>");
RequestDispatcher rd2=request.getRequestDispatcher("./Login.html");
rd2.include(request,response);
//or
//response.sendRedirect("./Login.html");
}
}
catch (SQLException e)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
}
}
的login.html
<!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>Login page</title>
</head>
<body>
<center>
<form action="./firstserv" method="post">
username
<input type="text" name="username" />
<br>
<br>
password
<input type="password" name="password"></input><br><br>
<input type="submit" value="login"></input>
<a href="./reg.html">new user</a>
</form>
</center>
</body>
</html>
reg.html
<!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>Registration Form</title>
</head>
<body>
registration page under construction...............
</body>
</html>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>UserDemo</display-name>
<servlet>
<description>
</description>
<display-name>SerExam</display-name>
<servlet-name>SerExam</servlet-name>
<servlet-class>
myPack.SerExam</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SerExam</servlet-name>
<url-pattern>/home.html</url-pattern>
</servlet-mapping>
<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>
答案 0 :(得分:0)
您的servlet映射到/home.html
:
<servlet-mapping>
<servlet-name>SerExam</servlet-name>
<url-pattern>/home.html</url-pattern>
</servlet-mapping>
当您访问firstserv
时:
<form action="./firstserv" method="post">
旁注:
永远不要在数据库中以纯文本存储密码:
ps=con.prepareStatement("select * from loginvalidation where username=? and password=?");
ps.setString(1, username);
ps.setString(2, password);
答案 1 :(得分:0)
将您的servlet映射标记更改为
<servlet-mapping>
<servlet-name>SerExam</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
然后您可以使用MyServlet
作为表单的操作参数,以便根据上面的servlet映射将带有表单参数的请求发送给您的servlet SerExam
。
<form action="MyServlet" method="post">
当您在表单中使用POST
方法时,将调用您的servlet doPost
方法,我可以从您的代码中看到您没有在doPost
方法中编写任何代码
删除doPost(request,response);
(这是doGet
方法中的第一行。
所以将servlet代码更改为:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//your existing code inside doGet with that doPost call removed
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
doGet(request,response);
}
在代码运行良好之前,还有许多事情需要完成。
因此,我会要求您查看我们的Servlet wiki,以便您获得基本的理解。