1.结构
![](https://i-blog.csdnimg.cn/blog_migrate/880827a075fd224b17cc9614826f134b.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4096cf22277f9819b1f855e711a88d16.png)
![](https://i-blog.csdnimg.cn/blog_migrate/fbc0e45a888dce637a57817fff8ac241.png)
2.代码
2.1 实体类
package com.taray.domain;
import java.util.Date;
//文章表
public class Know {
private int id;
private String title;
private String logo;
private String summary;//简介
private String tag;//标签
private Date esave;
private Date echg;
private int state;
//getter setter方法
}
package com.taray.domain;
import java.util.Date;
public class User {
private int id;
private String name;
private int age;
private String address;
private String email;
private Date birtyDay;
private String password;
}
2.2 service(就不写dao了)
package com.taray.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.taray.domain.Know;
import com.taray.util.DBConnUtil;
public class KnowService {
//获取共计多少条数据
public int getRowCount(){
//计算一共多少条
int rowcount=0;
ResultSet rscount=DBConnUtil.executeQuery("select count(*) from know", null);
try {
while(rscount.next()){
rowcount=rscount.getInt(1);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
return rowcount;
}
//常识列表分页
public List<Know> getKnowList(int pageNow,int pageSize){
List<Know> list=new ArrayList<Know>();
//查询分页语句
String sql="select * from know order by id asc limit "+(pageNow-1)*pageSize+","+pageSize;
ResultSet rs=DBConnUtil.executeQuery(sql, null);
Know know=null;
try {
while(rs.next()){
know=new Know();
know.setId(rs.getInt(1));
know.setTitle(rs.getString(2));
know.setLogo(rs.getString(4));
know.setEsave(rs.getDate(10));
list.add(know);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBConnUtil.close(DBConnUtil.getRs(), DBConnUtil.getPstmt(), DBConnUtil.getConn());
}
return list;
}
//删除单独某一条
public boolean delKnow(String id){
boolean b=false;
if(id!=null && !"".equals(id)){
String sql="delete from know where id=?";
String [] parameters={id};
b=DBConnUtil.executeUpdate(sql, parameters);
}
return b;
}
}
package com.taray.service;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.taray.domain.User;
import com.taray.util.DBConnUtil;
public class UserService {
/**
* 用户登录
* @return
* @throws SQLException
*/
public boolean UserLogin(User user) throws SQLException{
boolean b=false;
String sql="select * from user where name=? and password=?";
String [] parameters={user.getName(),user.getPassword()};
ResultSet rs=DBConnUtil.executeQuery(sql, parameters);
if(rs.next()){
b=true;
}
return b;
}
}
2.3 数据库工具类
package com.taray.util;
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.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
public class DBConnUtil {
private static Connection conn=null;
private static PreparedStatement pstmt=null;
private static ResultSet rs=null;
//连接数据库参数
private static String url="";
private static String username="";
private static String password="";
private static String driver="";
//读取配置文件
private static Properties pp=null;
private static InputStream ins=null;
//执行静态代码块
static{
try {
pp=new Properties();
ins=DBConnUtil.class.getClassLoader().getResourceAsStream("dbconn.properties");
pp.load(ins);
url=pp.getProperty("url");
username=pp.getProperty("user");
password=pp.getProperty("pass");
driver=pp.getProperty("driver");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
ins=null;
}
}
//得到连接
public static Connection getConnection(){
try {
conn=DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//关闭资源
public static void close(ResultSet rs,PreparedStatement pstmt,Connection conn){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
pstmt=null;
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn=null;
}
}
//统一的select
public static ResultSet executeQuery(String sql,String []parameters){
try {
conn=getConnection();
pstmt=conn.prepareStatement(sql);
if(parameters!=null&&!parameters.equals("")){
for(int i=0;i<parameters.length;i++){
pstmt.setString(i+1, parameters[i]);
}
}
rs=pstmt.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}finally{
//close(rs,pstmt,conn);
}
return rs;
}
//统一的executeUpdate
public static boolean executeUpdate(String sql,String []parameters){
boolean b=false;
int num=0;
try {
conn=getConnection();
pstmt=conn.prepareStatement(sql);
if(parameters!=null&&!parameters.equals("")){
for(int i=0;i<parameters.length;i++){
pstmt.setString(i+1, parameters[i]);
}
}
num=pstmt.executeUpdate();
if(num>0){
b=true;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
close(rs,pstmt,conn);
}
return b;
}
public static Connection getConn() {
return conn;
}
public static PreparedStatement getPstmt() {
return pstmt;
}
public static ResultSet getRs() {
return rs;
}
}
2.4 servlet
LoginServlet.java
package com.taray.view;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.taray.domain.User;
import com.taray.service.UserService;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public LoginServlet() {
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
User user=new User();
user.setName(request.getParameter("uname"));
user.setPassword(request.getParameter("upwd"));
UserService u=new UserService();
try {
boolean b=u.UserLogin(user);
if(b){
//1.(Cookie)如果验证账号密码正确,更新cookie中最后一次登录时间
boolean b1=false;
Cookie [] ck=request.getCookies();
if(ck!=null){
for(Cookie cookie:ck){
String cname=cookie.getName();
if("lasttime".equals(cname)){
System.out.println("上次登录:"+cookie.getValue());
//更新时间,把当前日期更新到cookie中
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String nowTime=sdf.format(new Date());
cookie.setValue(nowTime);
cookie.setMaxAge(7*3600*24);
response.addCookie(cookie);
b1=true;
break;
}
}
}
//2.cookie中没找到最后一次登录时间记录,添加
if(!b1){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String nowTime=sdf.format(new Date());
Cookie cook=new Cookie("lasttime",nowTime);
cook.setMaxAge(7*3600*24);
response.addCookie(cook);
}
//3.保存登录信息Session
HttpSession session=request.getSession();
session.setAttribute("loginuser", user);
//如果不保存JSSIONID默认是会话级别,关闭浏览器再开就找不到这个session了,这里把
//JSESSIONID存为30分钟的cookie,这样再打开浏览器还能够自动请求发送此JSESSIONID找到之前的session
Cookie cke=new Cookie("JSESSIONID",session.getId());
cke.setMaxAge(60*30);
response.addCookie(cke);
//4.保存访问数+1
String nums=(String)this.getServletContext().getAttribute("nums");
if(nums==null){
//说明这是第一个人
this.getServletContext().setAttribute("nums", "1");
}else{
this.getServletContext().setAttribute("nums", (Integer.parseInt(nums)+1)+"");
}
//request.getRequestDispatcher("/KnowServlet").forward(request, response);
response.sendRedirect("/UserManager2/servlets/KnowServlet");
}else{
request.setAttribute("err", "用户名或者密码错误!");
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
KnowServlet.java
package com.taray.view;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.taray.domain.Know;
import com.taray.service.KnowService;
/**
* Servlet implementation class KnowServlet
*/
public class KnowServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public KnowServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
KnowService knowService=new KnowService();
//1.计算共计多少页,第一种方法,第二种简单,记忆
//pagecount=rowcount%pagesize==0?rowcount/pagesize:rowcount/pagesize+1;
int rowcount=knowService.getRowCount();
int pagecount=(rowcount-1)/20+1;
int pagenow=1;
//获取当前多少页
String pn=request.getParameter("pn");
if(pn!=null&&!"".equals(pn)){
pagenow=Integer.parseInt(pn);
if(pagenow<=1){
pagenow=1;
}
}
List<Know> list=knowService.getKnowList(pagenow, 20);
//共计多少页
request.setAttribute("pagenow", pagenow);
request.setAttribute("pagecount", pagecount);
request.setAttribute("list", list);
request.getRequestDispatcher("/knowlist.jsp").forward(request, response);
}
}
DelServlet.java
package com.taray.view;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.taray.service.KnowService;
/**
* Servlet implementation class DelServlet
*/
public class DelServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DelServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String id=request.getParameter("id");
KnowService knowService=new KnowService();
boolean b=knowService.delKnow(id);
System.out.println("删除成功"+b);
response.sendRedirect("/UserManager2/servlets/KnowServlet");
//request.getRequestDispatcher("/servlets/KnowServlet").forward(request, response);
}
}
InitServlet.java
package com.taray.view;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class InitServlet
*/
//@WebServlet("/InitServlet")servlet3.0开始对于原来在web.xml定义的servlet,filter,listener,InitParam都可以通过annotation来配置了,而不需要在web.xml中定义。
public class InitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public InitServlet() {
super();
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);//重写前执行父级init方法,才能正常使用servletContext获取文件路径
//从record.txt读取浏览量
//1.首先读取文件的真实路径
String filePath=this.getServletContext().getRealPath("record.txt");
//2.打开文件
try {
FileReader fileReader=new FileReader(filePath);
//为了读取方便转为BufferReader
BufferedReader br=new BufferedReader(fileReader);
String nums=br.readLine();
this.getServletContext().setAttribute("nums",nums);
//流必须及时关闭
br.close();
fileReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @see Servlet#destroy()
* 正常的stop项目,而不是terminate方式关闭。才会正常执行
*/
public void destroy() {
//把servletContenxt值重新保存到文件,这个只是修改tomcat下该文件,如果重新发布又归零了
//从record.txt读取浏览量
//1.首先读取文件的真实路径
String filePath=this.getServletContext().getRealPath("record.txt");
BufferedWriter br=null;
FileWriter fileWriter=null;
//2.打开文件
try {
fileWriter=new FileWriter(filePath);
//为了读取方便转为BufferReader
br=new BufferedWriter(fileWriter);
//从servletContext读取访问量
String nums=(String) this.getServletContext().getAttribute("nums");
//写回文件
br.write(nums);
this.getServletContext().setAttribute("nums",nums);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
br.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入InitServlet");
}
}
对于原来在web.xml定义的servlet,filter,listener,InitParam都可以通过annotation来配置了,而不需要在web.xml中定义。
public class InitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public InitServlet() {
super();
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);//重写前执行父级init方法,才能正常使用servletContext获取文件路径
//从record.txt读取浏览量
//1.首先读取文件的真实路径
String filePath=this.getServletContext().getRealPath("record.txt");
//2.打开文件
try {
FileReader fileReader=new FileReader(filePath);
//为了读取方便转为BufferReader
BufferedReader br=new BufferedReader(fileReader);
String nums=br.readLine();
this.getServletContext().setAttribute("nums",nums);
//流必须及时关闭
br.close();
fileReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @see Servlet#destroy()
* 正常的stop项目,而不是terminate方式关闭。才会正常执行
*/
public void destroy() {
//把servletContenxt值重新保存到文件,这个只是修改tomcat下该文件,如果重新发布又归零了
//从record.txt读取浏览量
//1.首先读取文件的真实路径
String filePath=this.getServletContext().getRealPath("record.txt");
BufferedWriter br=null;
FileWriter fileWriter=null;
//2.打开文件
try {
fileWriter=new FileWriter(filePath);
//为了读取方便转为BufferReader
br=new BufferedWriter(fileWriter);
//从servletContext读取访问量
String nums=(String) this.getServletContext().getAttribute("nums");
//写回文件
br.write(nums);
this.getServletContext().setAttribute("nums",nums);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
br.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进入InitServlet");
}
}
AllFilter.java
package com.taray.view;
import java.io.IOException;
import java.util.regex.Pattern;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AllFilter implements Filter {
//要检查的session的名称
private String sessionKey;
//不拦截的URL正则表达式
private Pattern excepUrlPattern;
//检查不通过,转发的URl
private String forwardUrl;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
System.out.println("执行过滤器");
HttpServletRequest request=(HttpServletRequest)req;
HttpServletResponse response=(HttpServletResponse)res;
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("UTF-8");
String servletPath = request.getServletPath();
//如果请求路径与forwardurl相同,或请求的路径是排除的URL时,则直接放行
System.out.println(servletPath+"--"+excepUrlPattern.matcher(servletPath).matches());
if(servletPath.equals(forwardUrl)||excepUrlPattern.matcher(servletPath).matches()){
System.out.println("执行了");
chain.doFilter(req, res);
return;
}
Object sessionobj=request.getSession().getAttribute(sessionKey);
if(sessionobj==null){
request.setAttribute("err", "您还没有登录!");
request.getRequestDispatcher(forwardUrl).forward(request, response);
}else{
chain.doFilter(req, res);
}
}
@Override
public void init(FilterConfig cfg) throws ServletException {
sessionKey=cfg.getInitParameter("sessionKey");
String excepUrlRegex = cfg.getInitParameter("excepUrlRegex");
if(excepUrlRegex!=null && !"".equals(excepUrlRegex)){
//将给定正则表达式编译成一个模式
excepUrlPattern=Pattern.compile(excepUrlRegex);
}
forwardUrl=cfg.getInitParameter("redirectUrl");
}
}
TestServelt1.java 测试用,和登录无关
package com.taray.view;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 读取src webcontent webinfo下配置文件测试
*/
@WebServlet("/TestServlet")
public class TestServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet1() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
//http://blog.csdn.net/origin100/article/details/5808234 web项目文件读取
//1.读取WebContent下文件(根目录下)
InputStream ins=this.getServletContext().getResourceAsStream("dbconn1.properties");
//2.读取src目录下资源文件
InputStream ins2=this.getClass().getClassLoader().getResourceAsStream("dbconn.properties");
//3.读取web-inf下资源文件:获取web文件全路径,通过InputStream子类FileInputStream获取
String realPath=this.getServletContext().getRealPath("/WEB-INF/dbconn2.properties");
File file=new File(realPath);
FileInputStream fis=new FileInputStream(file);
//创建properties
Properties p=new Properties();
p.load(ins);
System.out.println(p.getProperty("url"));
Properties p2=new Properties();
p2.load(ins2);
System.out.println(p2.getProperty("user"));
Properties p3=new Properties();
p3.load(fis);
System.out.println(p3.getProperty("driver"));
}
}
dbconn.properties //dbconn1.properties dbconn2.properties在页面结构中和这个一样,是为了上面的测试程序用
url = jdbc:mysql://127.0.0.1:3306/test
driver =com.mysql.jdbc.Driver
user = root
pass = admin
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<!--检查用户是否登录了系统的过滤器配置 开始 -->
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>com.taray.view.AllFilter</filter-class>
<init-param>
<description>将当前登录的用户的信息保存在 session 中时使用的key,如果没有配置此参数,则该过滤器不起作用</description>
<param-name>sessionKey</param-name>
<param-value>loginuser</param-value>
</init-param>
<init-param>
<description>
如果用户未登录(即在 session 中 key 为 sessionKey 的属性不存在或为空),则将请求重定向到该 url。
该 url 不包含web应用的 ContextPath。
如果不配置此参数,则在用户未登录系统的情况下,直接重定向到web应用的根路径(/)
</description>
<param-name>redirectUrl</param-name>
<param-value>/index.jsp</param-value>
</init-param>
<init-param>
<description>
不需要进行拦截的 url 的正则表达式,即:如果当前请求的 url 的 servletPath 能匹配该正则表达式,则直接放行(即使未登录系统)。
此参数的值一般为 loginServlet 和 registServlet 等。
另外,参数 redirectUrl 的值不用包含在该正则表达式中,因为 redirectUrl 对应的 url 会被自动放行。
还有一点需要说明的是,该参数的值不包含web应用的 ContextPath。
</description>
<param-name>excepUrlRegex</param-name>
<!-- 不拦截 /servlets/loginServlet 和 /servlets/registServlet -->
<param-value>/servlets/(Login|Regist)Servlet</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/servlets/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/jsp/*</url-pattern>
</filter-mapping>
<!--检查用户是否登录了系统的过滤器配置 结束 -->
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.taray.view.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlets/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>KnowServlet</servlet-name>
<servlet-class>com.taray.view.KnowServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>KnowServlet</servlet-name>
<url-pattern>/servlets/KnowServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.taray.view.TestServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/servlets/TestServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DelServlet</servlet-name>
<servlet-class>com.taray.view.DelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DelServlet</servlet-name>
<url-pattern>/servlets/DelServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>com.taray.view.InitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>InitServlet</servlet-name>
<url-pattern>/InitServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp
<%@ 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>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</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">
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.js"></script>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<div>
<h1>用户登录</h1>
<!-- action应该这么写,/web应用名称/servlet的url -->
<form action="/UserManager2/servlets/LoginServlet" method="post">
用户名:<input type="text" name="uname"/><br/>
密 码:<input type="password" name="upwd"/><br/>
<input type="submit" class="btn btn-primary" value="登录"/>
<input type="button" class="btn btn-primary" value="取消"/>
</form>
<font style="color:red;">${err}</font>
</div>
</body>
</html>
knowlist.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.js"></script>
<title>查询商品列表</title>
</head>
<body style="text-align:center;">
<script type="text/javascript">
</script>
<div style="margin:0 auto;">
<h2>User列表(总计:${pagecount}页)</h2>
<div class="row">
<div class="col-md-12">
<table class="table table-bordered">
<tr>
<th>ID</th>
<th>标题</th>
<th>图片</th>
<th>操作</th>
</tr>
<c:forEach items="${list }" var="know">
<tr>
<td>${know.id }</td>
<td style="width:600px;">${know.title }</td>
<td><img src="" title="" width="40px" height="40px"/></td>
<td>
<a href="/UserManager2/servlets/DelServlet?id=${know.id }">删除</a>
<a href="">修改</a>
</td>
</tr>
</c:forEach>
</table>
<div style="font-size:20px">
<a href="/UserManager2/servlets/KnowServlet?pn=${pagenow-1}">上一页</a>
<c:if test="${pagecount le 5 }">
<a href="/UserManager2/servlets/KnowServlet?pn=1" <c:if test="${pagenow==1 }">style="color:red;"</c:if>>1</a>
<a href="/UserManager2/servlets/KnowServlet?pn=2" <c:if test="${pagenow==2 }">style="color:red;"</c:if>>2</a>
<a href="/UserManager2/servlets/KnowServlet?pn=3" <c:if test="${pagenow==3 }">style="color:red;"</c:if>>3</a>
<a href="/UserManager2/servlets/KnowServlet?pn=4" <c:if test="${pagenow==4 }">style="color:red;"</c:if>>4</a>
<a href="/UserManager2/servlets/KnowServlet?pn=5" <c:if test="${pagenow==5 }">style="color:red;"</c:if>>5</a>
</c:if>
<c:if test="${pagecount gt 5 }">
<c:if test="${pagenow le 3 }">
<a href="/UserManager2/servlets/KnowServlet?pn=1" <c:if test="${pagenow==1 }">style="color:red;"</c:if>>1</a>
<a href="/UserManager2/servlets/KnowServlet?pn=2" <c:if test="${pagenow==2 }">style="color:red;"</c:if>>2</a>
<a href="/UserManager2/servlets/KnowServlet?pn=3" <c:if test="${pagenow==3 }">style="color:red;"</c:if>>3</a>
<a href="/UserManager2/servlets/KnowServlet?pn=4" <c:if test="${pagenow==4 }">style="color:red;"</c:if>>4</a>
<a href="/UserManager2/servlets/KnowServlet?pn=5" <c:if test="${pagenow==5 }">style="color:red;"</c:if>>5</a>
</c:if>
<c:if test="${pagenow gt 3}">
<a href="/UserManager2/servlets/KnowServlet?pn=${pagenow-2 }">${pagenow-2 }</a>
<a href="/UserManager2/servlets/KnowServlet?pn=${pagenow-1 }">${pagenow-1 }</a>
<a href="/UserManager2/servlets/KnowServlet?pn=${pagenow }" style="color:red">${pagenow}</a>
<a href="/UserManager2/servlets/KnowServlet?pn=${pagenow+1 }">${pagenow+1 }</a>
<a href="/UserManager2/servlets/KnowServlet?pn=${pagenow+2 }">${pagenow+2 }</a>
</c:if>
</c:if>
<a href="/UserManager2/servlets/KnowServlet?pn=${pagenow+1}">下一页</a>
</div>
</div>
</div>
</div>
</body>
</html>
record.txt
0