Servlet是登陆验证的一个重要东西,我也只是记录下自己在做实验过程中的一些问题和感想。
这只是一个单纯的使用Servlet的实验,没有JSP、JavaBean等。
登录界面的代码就是个前端的代码,基本上没什么问题,就跟实验三的代码一样就可以,也可以稍微修改下布局。
然后是创建LoginServlet类文件,用于登录验证,其代码跟实验三的登录验证代码差不多,只是一个是.jsp,一个是.class,写法上面还是有一定的区别的。
以下是我本人自己的代码,仅供参考:
//代码中所有的try{}catch(){}如果不想自己添加可以只写try里面的内容,系统会提示自动补全 package Servlets; //创建类文件的时候自己创建的包,具体如图1 import java.io.IOException; 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{ /** * */ private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String url = "jdbc:mysql://localhost:3306/local?ServletTimezone=UTC"; //local为自己的数据库名,记得更改 注意:!!!问号后面一定要用Servlet不要用server,否侧绝对报错! String user="root"; String password="123456"; String DRIVER_PATH = "com.mysql.cj.jdbc.Driver"; try { Class.forName(DRIVER_PATH); } catch (ClassNotFoundException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } Connection con = null; try { con = (Connection)DriverManager.getConnection(url,user,password); } catch (SQLException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } Statement stmt = null; try { stmt = (Statement)con.createStatement(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } String sql = "SELECT * FROM user"; //从指定表中查找,user为自己数据库中的表 ResultSet rs = null; try { rs = stmt.executeQuery(sql); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String name=request.getParameter("username"); //此处username和password为登陆界面控件名 String pwd=request.getParameter("password"); String info =""; try { //if(rs.next()){ while(rs.next()) { if(name.equals(rs.getString("username"))&&pwd.equals(rs.getString("password"))){ //此处username和password为数据库表中的属性名 //String s = request.getParameter("username"); HttpSession session=request.getSession(); session.setAttribute("username",name); //这两行代码可以用上面那一行注释的代替 info = name+"欢迎登陆!" ; //可根据自己的需求改变提示词 request.setAttribute("outputMessage", info); request.getRequestDispatcher("/Info.jsp").forward(request,response); "/Info.jsp"为自己另外创建的一个.jsp,根据自己的需求命名,代码在下面 } else{ info = "用户名或密码错误,请重新登录!"; //可根据自己的需求改变提示词 } } request.setAttribute("outputMessage", info); request.getRequestDispatcher("/Info.jsp").forward(request,response); //这两行代码要放在while的外面,否则登录验证只能执行数据表中第一条数据,其他都会显示错误。 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { rs.close(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
图1:创建.class类界面
Package和Name是自己命名的。
如果出现Connection为null的错误可以参考
如果出现LoginServlet类出现错误的情况请仔细看清页面提示的根本信息,尤其是第一句。
我就是因为字符打错导致的错误,却因为没仔细看浪费了十几分钟。
Info.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%=request.getAttribute("outputMessage") %>
</body>
</html>
直接是创建新的.jsp后在<body> </body>中加入”outputMessage"用于验证登录的信息提示。
以上代码写完后不能够直接运行,否则还会报LoginServlet无法连接(类似这样)的错误,还需要在web.xml中加入这样一段代码:
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>Servlets.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
//LoginServlet为自己创建的Java类的名字,Servlets为创建Java类时的package
加入后基本上就不会出现什么问题,就可以直接运行,输入账号密码后进行登陆验证。
如果出现web.xml不存在的错误,可以参考
在创建项目时进行的操作。
或者不想重新创建项目可以参考
(2条消息) 利用eclipse新建的java web项目没有web.xml文件怎么办?_懵懂无知的蜗牛的博客-CSDN博客_没有web.xml文件怎么办
以上是我自己在做实验时的情况和想法,可能不是很好或者可能有些地方没有处理好,敬请原谅。
如果还出现其他别的错误可以交流讨论。