Servlet课程0425(五) sendRedirect实现不同页面共享数据
生活随笔
收集整理的這篇文章主要介紹了
Servlet课程0425(五) sendRedirect实现不同页面共享数据
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Login.java
//登錄界面 package com.tsinghua;import javax.servlet.http.*; import java.io.*;public class Login extends HttpServlet{public void doGet(HttpServletRequest req, HttpServletResponse res){//業(yè)務(wù)邏輯try{//中文亂碼//瀏覽器默認(rèn)ISO-8859res.setContentType("text/html;charset=gbk"); PrintWriter pw = res.getWriter();//返回登錄界面pw.println("<html>");pw.println("<body>");pw.println("<h1>登錄界面</h1>");pw.println("<form action='loginCl' method=post>");pw.println("用戶(hù)名<input type='text' name='username'/><br/>");pw.println("密碼<input type='password' name='userpwd'/><br/>");pw.println("<input type='submit' value='loing' /><br/>");pw.println("</form>");pw.println("</body>");pw.println("</html>");}catch(Exception ex){ex.printStackTrace();}}//處理get請(qǐng)求//req用于獲得客戶(hù)端(瀏覽器)的信息//res用于向 客戶(hù)端(瀏覽器)返回信息public void doPost(HttpServletRequest req, HttpServletResponse res){this.doGet(req,res); } }LoginCl.java
//登錄界面 package com.tsinghua;import javax.servlet.http.*; import java.io.*;public class LoginCl extends HttpServlet{public void doGet(HttpServletRequest req, HttpServletResponse res){//業(yè)務(wù)邏輯try{//接收用戶(hù)名和密碼String u = req.getParameter("username");String p = req.getParameter("userpwd");//驗(yàn)證if(u.equals("litao") && p.equals("litao")){//合法 //跳轉(zhuǎn)到welcomeres.sendRedirect("wel?uname="+u+"&upass="+p);}else{//不合法//寫(xiě)你要到的Servlet的那個(gè)URLres.sendRedirect("login"); }}catch(Exception ex){ex.printStackTrace();}}//處理get請(qǐng)求//req用于獲得客戶(hù)端(瀏覽器)的信息//res用于向 客戶(hù)端(瀏覽器)返回信息public void doPost(HttpServletRequest req, HttpServletResponse res){this.doGet(req,res); } }Welcome.java
//登錄界面 package com.tsinghua;import javax.servlet.http.*; import java.io.*;public class Welcome extends HttpServlet{public void doGet(HttpServletRequest req, HttpServletResponse res){//得到從logincl傳遞的用戶(hù)名和密碼//如果參數(shù)的名字寫(xiě)錯(cuò)了,則得到的是null空值String u = req.getParameter("uname");String p = req.getParameter("upass");//業(yè)務(wù)邏輯try{PrintWriter pw = res.getWriter();//返回登錄界面pw.println("Welcome,hello,"+u+" password="+p); }catch(Exception ex){ex.printStackTrace();}}//處理get請(qǐng)求//req用于獲得客戶(hù)端(瀏覽器)的信息//res用于向 客戶(hù)端(瀏覽器)返回信息public void doPost(HttpServletRequest req, HttpServletResponse res){this.doGet(req,res); } }?
轉(zhuǎn)載于:https://www.cnblogs.com/beautiful-code/p/5432824.html
總結(jié)
以上是生活随笔為你收集整理的Servlet课程0425(五) sendRedirect实现不同页面共享数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [改善Java代码]覆写equals方法
- 下一篇: Navicat(连接) -1之SSL 设