我试图创建一个简单的登录表单,它采用用户名和密码,检查 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{
}
}
登录.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>