Java Web登錄頁面,重要題和自己想出的代碼,用戶表和用戶類型表,導航欄【詩書畫唱】
3、創(chuàng)建一個用戶表user和用戶類型表userType(項目中的角色和權限問題)
user:id,act,pwd,tid
userType:tid,tname
創(chuàng)建登錄頁面login.jsp,登錄成功后將act和tid保存在session中
登錄成功后跳轉(zhuǎn)到manage.jsp頁面,頁面中有一個導航欄,有如下選項:
管理所有用戶
個人中心
購物車
要求當用戶類型是超級管理員時,能夠顯示出導航欄中的所有選項
當用戶類型是其他時,只顯示個人中心和購物車

create table user(
id int primary key auto_increment,
act varchar(100) ,
pwd varchar(100) ,
tid int
);
create table userType(
tid int primary key auto_increment,
tname varchar(100)?
);
insert into User(act ,pwd ,tid ) values ("詩書畫唱1","666",1),
?("詩書畫唱2","888",2);
insert into userType(tname) values ("超級管理員"),("其他");
--drop table? User
--select *? from User
--select *? from userType
select * from User inner? join? userType on User.tid= userType.tid




package bean;
public class User {
private Integer id;
private String act;
private String pwd;
private Integer tid;
private String tname;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAct() {
return act;
}
public void setAct(String act) {
this.act = act;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
}


package controller;
import bean.User;
import DAO.Dao;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.User;
import DAO.Dao;
@WebServlet("/UFirstPageServletStart")
public class FirstPageServletStart extends HttpServlet {
private static final long serialVersionUID = 1L;
??
? ? public FirstPageServletStart() {
? ? ? ? super();
? ? ? ? // TODO Auto-generated constructor stub
? ? }
/**
* @see HttpServlet#doGet(HttpServletRequest request,
*? HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,?
HttpServletResponse response)?
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)?
throws ServletException, IOException {
? ? String act = request.getParameter("act");
? ? String pwd = request.getParameter("pwd");
? ? System.out.println(act);
? ? //2、查詢數(shù)據(jù)庫
? ?Dao userDao = new Dao();
? ? User u = userDao.selectByActAndPwd(act, pwd);
? ? //3、根據(jù)查詢出來的結果進行處理
? ? if(u.getId() != null && u.getId() > 0) {
? ? //將登錄的賬號存放到session
? ? //后面當你跳轉(zhuǎn)到任何的頁面時,還需要驗證是否是合法的訪問
? ? request.getSession().setAttribute("userName", act);
? ? //a、根據(jù)賬號和密碼能夠查詢記錄,就表示登錄成功,跳轉(zhuǎn)到后臺管理頁面
? ? StringBuilder html = new StringBuilder();
if("超級管理員".equals(u.getTname())){
html.append(" <ul><li><a href='#'>管理所有用戶</a></li><li>"
+ "<a href='#'>個人中心</a></li><li>"
+ "<a href='#'>購物車</a></li></ul>");
}
else if("其他".equals(u.getTname())){
html.append(" <ul><li><a href='#'>個人中心</a></li>"
+ "<li><a href='#'>購物車</a></li></ul>");
}
System.out.println(html);
request.setAttribute("html",html);
request.getRequestDispatcher("firstPage.jsp").forward(request, response);
? ?
? ?
? ? } else {
? ? String msg = "賬號或者密碼錯誤";
? ? request.setAttribute("msg", msg);
? ? //b、沒有查詢到記錄,就表示登錄失敗,跳轉(zhuǎn)回login.jsp
? ? request.getRequestDispatcher("login.jsp")
? ? .forward(request, response);
? ? }? ?
}
}




package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import utils.DBUtils;
import bean.User;
public class Dao {
public User selectByActAndPwd
? ? (String act,String pwd){??
? ? String sql = "select * from user? u inner"
+ " join userType? ut on? u.tid=ut.tid"
+ " where act = ? and pwd = ?";
? ? Connection conn = null;
? ? PreparedStatement pstm = null;
? ? ResultSet rs = null;
User u = new User();
? ? List<User>list = new ArrayList<User>();
? ? try {
? ? ? ? conn = DBUtils.getConn();
pstm = conn.prepareStatement(sql);
//設置占位符
pstm.setString(1, act);
pstm.setString(2, pwd);
rs = pstm.executeQuery();
if(rs.next()) {
Integer id = rs.getInt("id");
Integer tid= rs.getInt("tid");
String tname= rs.getString("tname");
u.setId(id);
u.setAct(act);
u.setPwd(pwd);
u.setTid(tid);
u.setTname(tname);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//清理資源
DBUtils.close(rs, pstm, conn);
}
? ? return u;
? ? }
}


package Filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter("/*")
public class CodeFilter implements Filter {
? ?
? ? public CodeFilter() {
? ? ? ? // TODO Auto-generated constructor stub
? ? }
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request,?
ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}


package utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
public class DBUtils {
? ? private static String driverName;
? ? private static String url;
? ? private static String userName;
? ? private static String pwd;
? ? //靜態(tài)塊,隨著類加載而運行的
? ? static{
? ? //讀取db.properties文件中的內(nèi)容:
? ? Properties prop = new Properties();
? ? InputStream is = DBUtils.class.getClassLoader()
? ? .getResourceAsStream("db.properties");
? ? try {
prop.load(is);
driverName = prop.getProperty("dn");
url = prop.getProperty("url");
userName = prop.getProperty("un");
pwd = prop.getProperty("up");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
? ? }
? ??
? ? public static Connection getConn(){
? ? Connection conn = null;
? ? try {
Class.forName(driverName);
conn = DriverManager.getConnection(url,userName,pwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
? ? return conn;
? ? }
? ??
? ? public static void close(ResultSet rs,
? ? PreparedStatement pstm
? ? ,Connection conn){
? ? try {
? ? if(rs != null) {
? ? rs.close();
? ? }
? ? if(pstm != null) {
? ? pstm.close();
? ? }
? ? if(conn != null) {
? ? conn.close();
? ? }
? ? } catch(Exception e) {
? ? e.printStackTrace();
? ? }
? ? }
}


dn=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/firstjsp?useUnicode=true&characterEncoding=UTF-8
un=root
up=root

<%@page import="bean.User"%>
<%@page import="DAO.Dao"%>
<%@ page language="java" contentType="text/html;
?charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? //中文亂碼處理
? ? request.setCharacterEncoding("utf-8");
? ? //1、獲取用戶輸入的賬號和密碼
? ? String act = request.getParameter("act");
? ? String pwd = request.getParameter("pwd");
? ? System.out.println(act);
? ? //2、查詢數(shù)據(jù)庫
? ?Dao userDao = new Dao();
? ? User u = userDao.selectByActAndPwd(act, pwd);
? ? //3、根據(jù)查詢出來的結果進行處理
? ? if(u.getId() != null && u.getId() > 0) {
? ? //將登錄的賬號存放到session
? ? //后面當你跳轉(zhuǎn)到任何的頁面時,還需要驗證是否是合法的訪問
? ? request.getSession().setAttribute("userName", act);
? ? //a、根據(jù)賬號和密碼能夠查詢記錄,就表示登錄成功,跳轉(zhuǎn)到后臺管理頁面
? ? request.getRequestDispatcher("firstPage.jsp")
? ? .forward(request, response);
? ? } else {
? ? String msg = "賬號或者密碼錯誤";
? ? request.setAttribute("msg", msg);
? ? //b、沒有查詢到記錄,就表示登錄失敗,跳轉(zhuǎn)回login.jsp
? ? request.getRequestDispatcher("login.jsp")
? ? .forward(request, response);
? ? }? ?
%>


<%@ page language="java" contentType="text/html;
?charset=UTF-8" 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>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content=
? ? ? ? "keyword1,keyword2,keyword3">
? ? <meta http-equiv="description" content="This is my page">
? ? ?<style>
ul
{
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
background-color:black;
}
li
{
float:left;
padding:50px;
}
a
{
display:block;
text-decoration: none;
}
</style>
? ? ??
? ? </head>
? ? <body>
? ?${html }
? ? </body>
</html>


<%@ page language="java" contentType="text/html;?
charset=UTF-8" 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>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
?<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? ? ? <style type="text/css">
? ? ? ? ? ? *{
? ? ? ? ? ? ? ? font-size:50px;
? ? ? ? ? ? }
? ? ? ? </style>
? ? </head>
? ? <body>
? ? ? ? <form action="UFirstPageServletStart" method="post">
? ? ? ? ? ? <table border="1">
? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? <td>賬號:</td>
? ? <td><input type="text" name="act" value="${userName }"
? ? ? ? ? ? ? ? ? ? ? ? placeholder="請輸入賬號"/></td>
? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? <td>密碼:</td>
? ? ? ? ? ? ? ? ? ? <td><input type="password" name="pwd" /></td>
? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? <td colspan="2" align="center">
? ? ? ? ? ? ? ? ? ? ? ? <input type="submit" value="提交" />
? ? ? ? ? ? ? ? ? ? </td>
? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? </table>
? ? ? ? ? ? <div>${msg }</div>
? ? ? ? </form>
? ? </body>
</html>






