登陆页面jsp+servlet

虽然登陆界面很简单,但是自己老是忘记,所以写下,以后看的时候能够比较清楚的理解。

1.写jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
   <form action="${pageContext.request.contextPath}/LoginServlet" method="post" class="" role="form">
   username<input type="username" name="username" class="" id="username" placeholder="请输入用户名...">
   password<input type="password" name="password" class="" id="password" placeholder="请输入密码...">
   <span style="color:red">${msg }</span> 
   <input type="submit">
   </form>
</body>
</html>

说明:(1)action后面跟着的就是这个页面要验证的servlet类的位置

          (2)name=“username” name=“password”相当于一个标识在servlet中就是通过name值来对数据进行操作的

          (3)${msg }表示获取内置对象中赋值给msg变量的值

2.写对应的servlet,这里的servlet的名字与login.jsp中action后面跟着的名字必须一致。

package demo.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
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 LoginServlet extends HttpServlet {
	static final String driverClassName = "com.mysql.jdbc.Driver";
	static final String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
	static final String mysqlUsername = "root";
	static final String mysqlPassword = "slzslz";
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			Class.forName(driverClassName);
			conn = DriverManager.getConnection(url, mysqlUsername, mysqlPassword);
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String sql = "SELECT * FROM sign WHERE username=? and password=?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, username);
			pstmt.setString(2, password);
			rs = pstmt.executeQuery();
			System.out.println(username +", " + password);
			
			if(rs.next()) {
				request.setAttribute("username", rs.getString("username"));
				request.getSession().setAttribute("user_name", username);
				
				/*System.out.println(rs.getString("username"));*/
				request.setAttribute("msg", "成功登陆!");
				request.getRequestDispatcher("/success.jsp").forward(request, response);
			} else {
				request.setAttribute("msg", "用户名或密码不正确!");
				request.getRequestDispatcher("/login.jsp").forward(request,
						response);
			}
		} catch(Exception e) {
			throw new RuntimeException(e);
		} finally {
			try {
				if(conn != null) conn.close();
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();	
			} catch(Exception e) {
				throw new RuntimeException(e);
			}
		}	
	}
}

说明:(1)首先获取数据库的连接

         (2)request.setAttribute("msg", "成功登陆!");意思就是把成功登陆这句话当作一个变量赋值给内置对象msg,这个内置对象可以通过jsp页面直接调用。

         (3)在(2)的代码后面必须跟上request.getRequestDispatcher("/success.jsp").forward(request, response);表明我们所设置的内置对象值传给哪个jsp页面。

3.成功登陆跳转页面success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    ${msg }
</body>
</html>

4.配置文件web.xml

 <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>demo.web.servlet.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>

说明:(1)切记这里的<url-pattern>的值必须要与login.jsp页面的action后面跟的值的地址一致,否则一直会有报错的。

          (2)<servlet-class>后面跟的是包名+类名

5.页面效果展示

用户名或者密码错误,会提示用户名或者密码错误


正确登陆会跳转到success.jsp


Tips:这里只需要一个架包 mysql-connector-java-5.1.7-bin.jar即可。

猜你喜欢

转载自blog.csdn.net/weixin_42386538/article/details/80962610