Servlet course 0425 (vi) SKIP DIRECT JUMP---Session enables sharing of data between different pages

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.