JSP数据交互(一)

1.JSP内置对象

请求对象:request

输出对象:out

响应对象:response

应用程序对象:application

会话对象:session

页面上下文对象:pageContext

页面对象:page

配置对象:config

异常对象:exception

2.request对象的常用方法:

String  getParameter(String  name):根据页面表单组件名称获取页面提交数据

String [ ]   getParamterValues(String name):获取一组以相同   名称命名的表单组件 提交的数据

void setCharacterEncoding(String charset):  指定每个请求的编码,在调用request.getParameter()方法之前进行设定,可以用于解决中文乱码问题

RequestDispatcher getRequestDispatcher(String payh):返回一个javax.servlet.RequestDispacher对象,该对象的foeward()

index:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username.equals("lps") && password.equals("lucky")) {
response.sendRedirect("welcome.jsp");
} else {
out.println("请检查用户名和密码是否正确!");
}
%>
</body>
</html>

controller:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="controller.jsp" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>

welcome:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>欢迎您,来到贵美!</h1>
</body>
</html>

方法用于转发请求

上一篇:C++在设计和使用智能指针


下一篇:Sql Server 循环添加日期--(累加到一个字段中)