项目结构
效果演示
首先验证用户名,用户名密码正确则显示登陆成功的界面。然后记录访问次数,并且将访问访问量保存在本地文件中。而且,将编码格式配置在xml文件中,防止自己书写错误引起的编码错误的问题。
相关代码
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" id="WebApp_ID" version="2.5">
<!-- 配置全局属性,对请求和响应的编码格式进行配置 -->
<context-param>
<param-name>reqcode</param-name>
<param-value>utf-8</param-value>
</context-param>
<context-param>
<param-name>repscode</param-name>
<param-value>text/html;charset=utf-8</param-value>
</context-param>
<display-name>12Login</display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.bjsxt.servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.bjsxt.servlet.UserServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.bjsxt.servlet.MainServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>NumServlet</servlet-name>
<servlet-class>com.bjsxt.servlet.NumServlet</servlet-class>
<load-on-startup>1</load-on-startup><!-- 配置Servlet服务器启动加载 -->
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/user</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/main</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>NumServlet</servlet-name>
<url-pattern>/num</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
LoginServlet.java
package com.bjsxt.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//获取SevletContext对象
ServletContext sc = this.getServletContext();
//获取共享数据
String reqc = (String) sc.getInitParameter("reqcode");
String respc = (String) sc.getInitParameter("repscode");
//设置请求编码格式
req.setCharacterEncoding(reqc);
//设置响应编码格式
resp.setContentType(respc);
//获取session对象
HttpSession session = req.getSession();
String str= (session.getAttribute("flag")==null?"":"用户名或密码错误");
session.invalidate();
//获取请求信息
//处理请求信息
//相应处理结果
//直接响应
resp.getWriter().write("<html>");
resp.getWriter().write("<head>");
resp.getWriter().write("</head>");
resp.getWriter().write("<body>");
resp.getWriter().write("<form action='user' method='post'>");
resp.getWriter().write("<font color='red'>"+str+"</font><br />");
resp.getWriter().write("用户名:<input type='text' name='uname' /><br />");
resp.getWriter().write("密码: <input type='password' name='pwd' /><br />");
resp.getWriter().write("<input type='submit' value='登陆'>");
resp.getWriter().write("</form>");
resp.getWriter().write("</body>");
resp.getWriter().write("</html>");
}
}
UserServlet .java
package com.bjsxt.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class UserServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//获取SevletContext对象
ServletContext sc = this.getServletContext();
//获取共享数据
String reqc = (String) sc.getInitParameter("reqcode");
String respc = (String) sc.getInitParameter("repscode");
//设置请求编码格式
req.setCharacterEncoding(reqc);
//设置响应编码格式
resp.setContentType(respc);
//获取请求信息
//处理请求信息
String uname=req.getParameter("uname");
String pwd=req.getParameter("pwd");
if ("gaoqi".equals(uname)&&"123456".equals(pwd)) {
//登陆成功
//获取servletContext对象
//ServletContext sc = this.getServletContext();
//从sc对象中获取计数器,没有就创建
Object obj = sc.getAttribute("nums");
//判断
if (obj!=null) {
//计数器自增
int nums=(int) obj;
nums++;
//存储计数器数据到servletContext对象上
sc.setAttribute("nums", nums);
}else {
sc.setAttribute("nums", 1);
}
//创建session对象,获取用户名信息
HttpSession hs = req.getSession();
//hs是一个map,即键值对的集合,键名需要加引号,后来使用键名获取值时也要加引号
hs.setAttribute("uname", uname);
//响应处理结果
resp.sendRedirect("main");
}else {
//登录失败
//创建session并增加登陆失败标记
HttpSession session = req.getSession();
session.setAttribute("flag", "loginfalse");
//响应处理结果
resp.sendRedirect("login");
}
}
}
MainServlet .java
package com.bjsxt.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MainServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//获取SevletContext对象
ServletContext sc = this.getServletContext();
//获取共享数据
String reqc = (String) sc.getInitParameter("reqcode");
String respc = (String) sc.getInitParameter("repscode");
//设置请求编码格式
req.setCharacterEncoding(reqc);
//设置响应编码格式
resp.setContentType(respc);
//获取请求信息
//获取servletContext对象
//ServletContext sc = this.getServletContext();
//获取计数器数据
Object nums = sc.getAttribute("nums");
//处理请求信息
//响应请求结果
//直接响应
resp.getWriter().write("<html>");
resp.getWriter().write("<head>");
resp.getWriter().write("</head>");
resp.getWriter().write("<body>");
resp.getWriter().write("<h2>"+"欢迎"+req.getSession().getAttribute("uname")+"进入时间静止不是简史的博客"+"</h2>");
resp.getWriter().write("<hr />");
resp.getWriter().write("当前网页访问次数为:"+nums);
resp.getWriter().write("</body>");
resp.getWriter().write("</html>");
//请求转发
//重定向
}
}
NumServlet .java
package com.bjsxt.servlet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class NumServlet extends HttpServlet {
//在服务器启动时将num.txt中的数据存储到ServletContext中去
public void init() throws ServletException {
//获取ServletContext对象
ServletContext sc = this.getServletContext();
//获取num.txt中存储的浏览器浏览的次数
//获取nums.txt的绝对路径
String path = sc.getRealPath("num/nums.txt");
//创建File对象
File f = new File(path);
//读取nums.txt中的数据
//获取资源流对象
FileInputStream fis=null;
InputStreamReader isr=null;
BufferedReader br=null;
try {
fis=new FileInputStream(f);
isr=new InputStreamReader(fis);
br=new BufferedReader(isr);
//读取
String nums = br.readLine();
//将数据存储到ServletContext中
sc.setAttribute("nums", Integer.parseInt(nums));
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//在服务器关闭的时候,将nums.txt中的数据存储到servletContext中
public void destroy() {
//获取ServletContext对象
ServletContext sc = this.getServletContext();
//获取计数器数据
int nums = (int) sc.getAttribute("nums");
//将计数器数据存储到nums.txt文件中
//获取nums.txt的绝对路径
String path = sc.getRealPath("num/nums.txt");
//获取file对象
File f = new File(path);
//获取资源流对象
FileOutputStream fos=null;
OutputStreamWriter osw=null;
BufferedWriter bw=null;
try {
fos=new FileOutputStream(f);
osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
//将数据写入到num.txt中
bw.write(nums+"");
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
源码分享:
链接:https://pan.baidu.com/s/1LNtaEn7RgceO1Zez9DyLRA
提取码:w739
复制这段内容后打开百度网盘手机App,操作更方便哦