Enter Http://localhost:8080/myWebSite/wel directly in the address bar
Will find the page can also jump, but the user name and password are empty, this is not possible, because there is no verified illegal login
Welcome,hello,nullpassword=null
Use the session to prevent users from logging in illegally
Login.java
//Login Interface PackageCom.tsinghua;Importjavax.servlet.http.*;ImportJava.io.*; Public classLoginextendshttpservlet{ Public voiddoget (httpservletrequest req, httpservletresponse res) {//Business Logic Try{ //Chinese garbled//Browser Default ISO-8859Res.setcontenttype ("TEXT/HTML;CHARSET=GBK"); PrintWriter PW=Res.getwriter (); //Back to login screenPw.println ("); Pw.println ("<body>"); Pw.println ("); Pw.println ("<form action= ' Logincl ' method=post>"); Pw.println ("User name <input type= ' text ' name= ' username '/><br/>"); Pw.println ("Password <input type= ' password ' name= ' userpwd '/><br/> '); Pw.println ("<input type= ' submit ' value= ' Loing '/><br/>"); Pw.println ("</form>"); Pw.println ("</body>"); Pw.println ("); } Catch(Exception ex) {ex.printstacktrace (); } } //Handling GET Requests//req for obtaining client (browser) information//Res is used to return information to the client (browser) Public voidDoPost (httpservletrequest req, httpservletresponse res) { This. Doget (Req,res); } }
Logincl.java
//Login Interface PackageCom.tsinghua;Importjavax.servlet.http.*;ImportJava.io.*; Public classLoginclextendshttpservlet{ Public voiddoget (httpservletrequest req, httpservletresponse res) {//Business Logic Try{ //receive user name and passwordString u = req.getparameter ("username"); String P= Req.getparameter ("Userpwd"); //Validation if(U.equals ("Litao") && p.equals ("Litao"))){ //Legal//writes the validation success information to the session//get the session, get the table of the sessionHttpSession HS = Req.getsession (true); //Default 30 minutes, modify destroy time below to show effect//This method is calculated in seconds.Hs.setmaxinactiveinterval (20); //Write PropertyHs.setattribute ("Pass", "OK"); //Jump to welcomeRes.sendredirect ("wel?uname=" +u+ "&upass=" +p); } Else{ //Not legal//write the URL of the servlet you want to go toRes.sendredirect ("Login"); } } Catch(Exception ex) {ex.printstacktrace (); } } //Handling GET Requests//req for obtaining client (browser) information//Res is used to return information to the client (browser) Public voidDoPost (httpservletrequest req, httpservletresponse res) { This. Doget (Req,res); } }
Welcome.java
//Login Interface PackageCom.tsinghua;Importjavax.servlet.http.*;ImportJava.io.*; Public classWelcomeextendshttpservlet{ Public voiddoget (httpservletrequest req, httpservletresponse res) {//get the session, get the table of the sessionHttpSession HS = Req.getsession (true); String Val= (String) hs.getattribute ("Pass"); //Judging if(val = =NULL) { Try { //Illegal LoginRes.sendredirect ("Login"); }Catch(Exception ex) {ex.printstacktrace (); } } //get the user name and password passed from Logincl//if the name of the parameter is incorrectly written, the resulting null null valueString u = req.getparameter ("uname"); String P= Req.getparameter ("UPass"); //Business Logic Try{printwriter pw=Res.getwriter (); //Back to login screenPw.println ("Welcome,hello," +u+ "password=" +p); } Catch(Exception ex) {ex.printstacktrace (); } } //Handling GET Requests//req for obtaining client (browser) information//Res is used to return information to the client (browser) Public voidDoPost (httpservletrequest req, httpservletresponse res) { This. Doget (Req,res); } }
Servlet course 0425 (vi) SKIP DIRECT JUMP---Session enables sharing of data between different pages