JSP第5-6周作业
1.安装MySQL数据库,建立用户表 uid uname upwd 并插入3条数据
2.制作jsp登录页面 login.jsp 提交到dologin.jsp,使用jdbc连数据库,判断输入的用户名密码是否存在
3.如果存在,把用户名保存在SESSION中,跳转到welcome.jsp,welcome.jsp中读取session中的用户名,显示欢迎你xxx
4.若不存在,跳到登录页面。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <style> body{ width:600; height:500px; overflow: hidden; position: relative; background-repeat: no-repeat; background-image:url(chengshi_yejing-007.jpg); } div{ width: 400px; height: 150px; padding-top: 10px; margin: 50px auto; background: #00000000; position:relative; top:100px; } form{ width: 300px; height: 150px; padding-top: 10px; margin: 50px auto; background: #00000000; border-radius: 20px; border: 3px solid #f8fcfc; opacity: 0.9; } </style> <head> </head> <% Integer count = (Integer)application.getAttribute("count"); if(count!=null){ count = 1+count; }else{ count = 1; } application.setAttribute("count",count); %> <body> <div> <form action="loginn.jsp" method="post"> 账号:<input type="text" name="name"><br> 密码:<input type="password" name="password" ><br> 验证码:<input type="text" name="yanzheng" > <input type="text" name="ma" value="4500" readonly="readonly"><br> <input type="submit" value="登录"> </form> </div> </body> </html>
<%@page import="java.nio.channels.SeekableByteChannel"%>
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<html>
<head>
</head>
<body>
<%!public static final String DBDRIVER = "com.mysql.jdbc.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/userDatabase";
public static final String DBUSER = "root";
public static final String DBPASS = "123456";%>
<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
boolean flag = false;
String name = null;
%>
<%
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
String sql = "SELECT uname FROM num_one WHERE uid=? AND upwd=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, request.getParameter("id"));
pstmt.setString(2, request.getParameter("password"));
rs = pstmt.executeQuery();
if (rs.next()) {
name = rs.getString(1);
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
if (flag) {
request.getSession().setAttribute("uname",name);
request.getRequestDispatcher("welcome.jsp").forward(request,response);
// request.getRequestDispatcher("welcome.jsp").forward(request, response);
} else {
response.sendRedirect("fail.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <% String Name = request.getParameter("name"); String Password = request.getParameter("password"); if(Name.equals("zs")&&Password.equals("123")){ request.getRequestDispatcher("welcome.jsp").forward(request,response); }else{ request.getRequestDispatcher("fail.jsp").forward(request,response); } %> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% String name = (String)session.getAttribute("name"); %> 欢迎你<% out.print("zs");%> </body> </html>



浙公网安备 33010602011771号