Jsp中request的3个基础实践

本文包含request内置对象的使用、乱码处理的两种方法、使用request.getParamter()方法获取表单提交的数据、采用request对象通过getParameter()方法和getParameterValues()方法获取表单请求数据、使用request内置对象时,注意类型转换、空指针异常。

实验要求1

设计并实现一个用户登录的过程,其中login.jsp页面提供一个表单,用于用户输入相应的用户名和密码进行登录,表单提交至checklogin.jsp页面,checklogin.jsp用于登录验证,检查用户名和密码是否正确,如果用户输入用户名computer,密码jsp后,则使用用<jsp:forward>动作标记跳转到success.jsp页面,否则,跳转到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> <th colspan="2">用户登录</th> <tr> <td>用户名</td> <td><input type="text" /></td> </tr> <tr> <td>密码</td> <td> <input type="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>

实验截图

Jsp中request的3个基础实践

实验要求2

编写一个JSP页面input.jsp,该页面提供一个表单,用户通过表单输入两个整数,及四则运算符号,提交表单至count.jsp页面,该页面负责根据选择的运算符计算出结果。

实验代码

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>输入运算数、选择运算符号:</h2> <input type="text" /> <select size='1px' /> <option>+</option> <option>-</option> <option>*</option> <option>/</option> </select> <input type="text" /> <br/> <br/> <input type="submit" value="运行结算结果" /> </form> </body> </html>

count.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> <h2>计算结果: <% 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>

实验截图

Jsp中request的3个基础实践

实验要求3

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzjzwd.html