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//Jump to welcomeRes.sendredirect ("wel?uname=" +u+ "&upass=" +Q); } 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 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 (Fri) sendredirect sharing data across different pages