前言
本文包含request內(nèi)置對象的使用、亂碼處理的兩種方法、使用request.getParamter()方法獲取表單提交的數(shù)據(jù)、采用request對象通過getParameter()方法和getParameterValues()方法獲取表單請求數(shù)據(jù)、使用request內(nèi)置對象時,注意類型轉(zhuǎn)換、空指針異常。
實驗要求1
設(shè)計并實現(xiàn)一個用戶登錄的過程,其中l(wèi)ogin.jsp頁面提供一個表單,用于用戶輸入相應(yīng)的用戶名和密碼進行登錄,表單提交至checklogin.jsp頁面,checklogin.jsp用于登錄驗證,檢查用戶名和密碼是否正確,如果用戶輸入用戶名computer,密碼jsp后,則使用用jsp:forward>動作標(biāo)記跳轉(zhuǎn)到success.jsp頁面,否則,跳轉(zhuǎn)到fail頁面。
實驗代碼
login.jsp
%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> html> head> title>用戶登錄/title> /head> body> br/> form action="checklogin.jsp" method="POST" target="_blank"> table border="1" width="500px" align="center"> th colspan="2">用戶登錄/th> tr> td>用戶名/td> td>input type="text" name="names" />/td> /tr> tr> td>密碼/td> td> input type="password" name="password" />/td> /tr> tr> td>input type="submit" value="提交" />/td> td>input type="reset" value="重置" />/td> /tr> /table> /form> /body> /html>
checklogin.jsp
%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> html> head>/head> body> % String user = request.getParameter("names"); String password = request.getParameter("password"); if(user.equals("computer")){ if(password.equals("jsp")){ %> jsp:forward page="./success.jsp">/jsp:forward> % }else{ %> jsp:forward page="./fail.jsp">/jsp:forward> % } }else{ %> jsp:forward page="./fail.jsp">/jsp:forward> % } %> /body> /html>
success.jsp
%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> html> head> title>success/title> /head> body> h1>success!/h1> /body> /html>
fail.jsp
%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> html> head> title>success/title> /head> body> h1>fail!/h1> /body> /html>
實驗截圖
實驗要求2
編寫一個JSP頁面input.jsp,該頁面提供一個表單,用戶通過表單輸入兩個整數(shù),及四則運算符號,提交表單至count.jsp頁面,該頁面負責(zé)根據(jù)選擇的運算符計算出結(jié)果。
實驗代碼
input.jsp
%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> html> head> title>簡單計算器/title> style> body { background-color: yellow; } /style> /head> body> form action="count.jsp" method="POST"> h2>輸入運算數(shù)、選擇運算符號:/h2> input type="text" name="a" /> select size='1px' name="b" /> option>+/option> option>-/option> option>*/option> option>//option> /select> input type="text" name="c" /> br/> br/> input type="submit" value="運行結(jié)算結(jié)果" /> /form> /body> /html>
count.jsp
%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> html> head> title>計算結(jié)果/title> style> body { background-color: yellow; } /style> /head> body> h2>計算結(jié)果: % String stra=request.getParameter("a"); String strb=request.getParameter("b"); String strc=request.getParameter("c"); float fa = Float.parseFloat(stra); float fc = Float.parseFloat(strc); System.out.print(strb); if(strb.equals("+")){ out.print(fa+strb+fc+"="+(fa+fc)); }else if(strb.equals("-")){ out.print(fa+strb+fc+"="+(fa-fc)); }else if(strb.equals("*")){ out.print(fa+strb+fc+"="+(fa*fc)); }else{ out.print(fa+strb+fc+"="+(fa/fc)); } %> /h2> /body> /html>
實驗截圖
實驗要求3
亂碼問題:編寫兩個JSP頁面,分別是question.jsp和answer.jsp
要求在question.jsp頁面里利用表單,提供如下頁面,提交表單至answer.jsp頁面,在answer.jsp頁面實現(xiàn)判斷用戶回答是否正確。
實驗代碼
question.jsp
%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> html> head> title>問題頁面/title> style> body { background-color: pink; } h2 { color: blue; } /style> /head> body> form action="answer.jsp" method="POST"> h2>小說圍城的作者是:/h2> input type="radio" name="a" value="錢鐘書">A.錢鐘書 input type="radio" name="a" value="海巖">B.海巖 input type="radio" name="a" value="路遙">C.路遙 input type="radio" name="a" value="韓寒">D.韓寒 br> h2>你意愿的工作城市:/h2> input type="checkbox" name="b" value="北京">A.北京 input type="checkbox" name="b" value="天津">B.天津 input type="checkbox" name="b" value="上海">C.上海 input type="checkbox" name="b" value="黃驊">D.黃驊 br> h2>請輸入姓名:/h2> input type="text" name="name"> input type="submit" value="提交驗證"> /form> /body> /html>
answer.jsp
%@page import="javax.servlet.annotation.HandlesTypes"%> %@page import="java.util.Enumeration"%> %@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> html> head> title>回答結(jié)果/title> style> body { background-color: #90bbde; } /style> /head> body> h2> % String str = request.getParameter("a"); String strtemp = new String(str.getBytes("iso-8859-1"),"UTF-8"); System.out.print(strtemp); String temp = new String("錢鐘書".getBytes("iso-8859-1"),"UTF-8"); if(strtemp.equals("錢鐘書")){ String name1 =request.getParameter("name"); String nametemp = new String(name1.getBytes("iso-8859-1"),"UTF-8"); %> 恭喜你, %= nametemp %> 回答正確,加兩分! % }else{ %> 很遺憾,回答錯誤! % } String[] strb=request.getParameterValues("b"); %> br> 你意愿的工作有 %= strb.length %>個,分別是: % for(int i=0;istrb.length;i++){ String strbtemp = new String(strb[i].getBytes("iso-8859-1"),"UTF-8"); out.print(" "+strbtemp); } %> /h2> /body> /html>
實驗截圖
標(biāo)簽:貸款邀約 內(nèi)江 銅川 樂山 南京 大連 黃石 廣州
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Jsp中request的3個基礎(chǔ)實踐》,本文關(guān)鍵詞 Jsp,中,request,的,3個,基礎(chǔ),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。