Use ServletContext to implement web page counters
(1) Train of Thought Analysis:
Use ServletContext and notepad to write the login times in the txt file. First, use the Servlet init () when the function is initialized, open the txt counter file and read the file. Then, when the user logs on, the website will not be accessed again and the technology will only count once, until after the cancellation, destroy () and Add 1 to the counter file;
(2) login interface
Package com. tsinghua; import javax. servlet. http. *; import java. io. *; public class Login extends HttpServlet {public void doGet (HttpServletRequest req, HttpServletResponse res) {// business logic try {// Chinese garbled res. setContentType (text/html; charset = gbk); PrintWriter pw = res. getWriter (); // return to the logon interface pw. println (
); Pw. println (); pw. println ( ); // Get the error message String info = req. getParameter (info); if (info! = Null) {pw. println (your user name or password is incorrect!
);} Pw. println (logon Interface); pw. println (); pw. println ( ); Pw. println (); pw. println ();} catch (Exception ex) {ex. printStackTrace () ;}} public void doPost (HttpServletRequest req, HttpServletResponse res) {this. doGet (req, res );}}
(2) process login page requests
Package com. tsinghua; import javax. servlet. http. *; import java. io. *; import java. SQL. *; public class LoginCl extends HttpServlet {// rewrite the init function public void init () {try {// will only be called once // create a FileReaderFileReader f = new FileReader (f: \ myCounter.txt); BufferedReader br = new BufferedReader (f); // read a row of data String numVal = br. readLine (); // be sure to close the file stream br. close (); // put the times value into servletcontextthis. getServletContext (). setAttribute (visi TTimes, numVal); System. out. println (inti called);} catch (Exception ex) {ex. printStackTrace () ;}/// rewrite the destroy function public void destroy () {try {// when writing new times, FileWriter fw = new FileWriter (f: \ myCounter.txt); BufferedWriter bw = new BufferedWriter (fw); // write data bw to the file. write (this. getServletContext (). getAttribute (visitTimes ). toString (); // close the object stream bw. close (); System. out. println (destroy called);} catch (Exception ex) {ex. PrintStackTrace () ;}/// process get requests // req: used to obtain information about the client (browser) // res: used to send data to the client (browser) public void doGet (HttpServletRequest req, HttpServletResponse res) {// business logic Connection ct = null; Statement sm = null; ResultSet rs = null; try {// accept username and password String u = req. getParameter (usrename); String p = req. getParameter (passwd); // call UserBeanCl, 1. create an object UserBeanCl ubc = new UserBeanCl (); // 2. use the UserBeanCl method if (ubc. checkUser (u, p) {// valid user String kee P = req. getParameter (keep); if (keep! = Null) {// Save the user name and password on the client (cookie) // create Cookie name = new Cookie (myname, u); Cookie pass = new Cookie (mypasswd, p); // set the time name. setMaxAge (14*24*3600); pass. setMaxAge (14*24*3600); // write back to the client res. addCookie (name); res. addCookie (pass);} // put the user name and password into the session. HttpSession hs = req. getSession (true); hs. setMaxInactiveInterval (30); hs. setAttribute (uname, u); // set the value of visitTime in serlvetContext to + + String times = this. getServletContext (). getA Ttritimes (visitTimes ). toString (); // pair times ++ and then replace servletthis. getServletContext (). setAttribute (visitTimes, (Integer. parseInt (times) + 1) +); // jump to welres. sendRedirect (Main);} else {// invalid // jump to res. sendRedirect (login); // write the url of the servlet You Want To Get} catch (Exception ex) {ex. printStackTrace ();} finally {try {if (rs! = Null) {rs. close () ;}if (sm! = Null) {sm. close () ;}if (ct! = Null) {ct. close () ;}} catch (Exception ex) {ex. printStackTrace () ;}}// process post requests // req: used to obtain client (browser) Information // res: used to send to client (browser) public void doPost (HttpServletRequest req, HttpServletResponse res) {this. doGet (req, res );}}
(3) submit the information to the wel page after successful login
Package com. tsinghua; import javax. servlet. http. *; import java. io. *; import java. SQL. *; import java. util. *; public class Wel extends HttpServlet {// process get requests // req: used to obtain client (browser) Information // res: used to send to client (browser) public void doGet (HttpServletRequest req, HttpServletResponse res) {// business logic Connection ct = null; PreparedStatement ps = null; ResultSet rs = null; // business logic try {// obtain the username HttpSession hs = req from the session. getSession (true); String MyName = (String) hs. getAttribute (uname); String name =; String passwd =; if (myName = null) {// if the session contains no user information, check whether there is any cookie information. // obtain all cookie information from the client. Cookie [] allCookies = req. getCookies (); int I = 0; // If allCookies are not empty... if (allCookies! = Null) {// extract cookiefor (I = 0; I) from it; // Add the webpage access count function pw. println (the webpage is accessed + this. getServletContext (). getAttribute (visitTimes ). toString () + times); pw. println ();} catch (Exception ex) {ex. printStackTrace () ;}} public void doPost (HttpServletRequest req, HttpServletResponse res) {this. doGet (req, res );}}