2021.4.7jsp第六周作业
1.安装MySQL数据库,建立用户表 uid uname upwd 并插入3条数据
2.制作jsp登录页面 login.jsp 提交到dologin.jsp,使用jdbc连数据库,判断输入的用户名密码是否存在
3.如果存在,把用户名保存在SESSION中,跳转到welcome.jsp,welcome.jsp中读取session中的用户名,显示欢迎你xxx
4.若不存在,跳到登录页面。
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form action="doLogin.jsp" method="post" > 用户名:<input type="text" name="username"/> <br> 密码:<input type="password" name="password"/> <br> <input type="submit" value="登录"> </form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> </head> <body> <% String name = (String) request.getSession().getAttribute("uname"); %> <body style="background:url(images/1.jpg)"> <div class="contentArea"> <h1 style="color: red">欢迎登录<%=name%>登陆!</h1> </html>
<% JDBCKu jdbcKu = new JDBCKu(); String username = request.getParameter("username"); String password = request.getParameter("password"); Connection conn = jdbcKu.getConnection(); String sql = "select * from user where uname = ? and upassword = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setNString(1,username); ps.setNString(2,password); ResultSet rs = ps.executeQuery(); if (rs.next() == true) { request.getSession().setAttribute("username",username); request.getRequestDispatcher("welcome.jsp").forward(request,response); }else{ response.sendRedirect("index.jsp"); } jdbcKu.closeAll(conn, ps, rs); %>
@WebServlet(name = "CreateCodeController",value = "/createCode") public class CreateCodeController extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ValidateCode code = new ValidateCode(200,30,4,20); String codes = code.getCode(); HttpSession session = request.getSession(); session.setAttribute("codes",codes); code.write(response.getOutputStream()); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }