Jsp中request的3個(gè)基礎(chǔ)實(shí)踐
前言
本文包含request內(nèi)置對(duì)象的使用、亂碼處理的兩種方法、使用request.getParamter()方法獲取表單提交的數(shù)據(jù)、采用request對(duì)象通過getParameter()方法和getParameterValues()方法獲取表單請(qǐng)求數(shù)據(jù)、使用request內(nèi)置對(duì)象時(shí),注意類型轉(zhuǎn)換、空指針異常。
實(shí)驗(yàn)要求1
設(shè)計(jì)并實(shí)現(xiàn)一個(gè)用戶登錄的過程,其中l(wèi)ogin.jsp頁面提供一個(gè)表單,用于用戶輸入相應(yīng)的用戶名和密碼進(jìn)行登錄,表單提交至checklogin.jsp頁面,checklogin.jsp用于登錄驗(yàn)證,檢查用戶名和密碼是否正確,如果用戶輸入用戶名computer,密碼jsp后,則使用用<jsp:forward>動(dòng)作標(biāo)記跳轉(zhuǎn)到success.jsp頁面,否則,跳轉(zhuǎn)到fail頁面。
實(shí)驗(yàn)代碼
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>
實(shí)驗(yàn)截圖
實(shí)驗(yàn)要求2
編寫一個(gè)JSP頁面input.jsp,該頁面提供一個(gè)表單,用戶通過表單輸入兩個(gè)整數(shù),及四則運(yùn)算符號(hào),提交表單至count.jsp頁面,該頁面負(fù)責(zé)根據(jù)選擇的運(yùn)算符計(jì)算出結(jié)果。
實(shí)驗(yàn)代碼
input.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <html> <head> <title>簡單計(jì)算器</title> <style> body {background-color: yellow; } </style> </head> <body> <form action="count.jsp" method="POST"> <h2>輸入運(yùn)算數(shù)、選擇運(yùn)算符號(hào):</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="運(yùn)行結(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>計(jì)算結(jié)果</title> <style> body {background-color: yellow; } </style> </head> <body> <h2>計(jì)算結(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>
實(shí)驗(yàn)截圖
實(shí)驗(yàn)要求3
亂碼問題:編寫兩個(gè)JSP頁面,分別是question.jsp和answer.jsp
要求在question.jsp頁面里利用表單,提供如下頁面,提交表單至answer.jsp頁面,在answer.jsp頁面實(shí)現(xiàn)判斷用戶回答是否正確。
實(shí)驗(yà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>請(qǐng)輸入姓名:</h2> <input type="text" name="name"> <input type="submit" value="提交驗(yàn)證"> </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{ %>很遺憾,回答錯(cuò)誤!<% } String[] strb=request.getParameterValues("b"); %> <br> 你意愿的工作有 <%= strb.length %>個(gè),分別是: <% for(int i=0;i<strb.length;i++){ String strbtemp = new String(strb[i].getBytes("iso-8859-1"),"UTF-8"); out.print(" "+strbtemp); } %></h2> </body> </html>
實(shí)驗(yàn)截圖
相關(guān)文章:
1. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲2. jsp實(shí)現(xiàn)簡單用戶7天內(nèi)免登錄3. SpringBoot如何使用RequestBodyAdvice進(jìn)行統(tǒng)一參數(shù)處理4. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式5. 如何在jsp界面中插入圖片6. jsp中sitemesh修改tagRule技術(shù)分享7. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除8. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫的方法9. JSP取得在WEB.XML中定義的參數(shù)10. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
