JavaWeb -jsp文件和内置对象的解析

 jsp文件和内置对象的解析

对page解析

 

JSP九大内置对象(自带,无需new)

1 out:输出对象

2 request:请求对象,存储“客户端像服务端发送的请求信息”

3 response:响应对象

4 pageContext

5 session

6 application

7 config

8 page

9 exception

两种提交文件方式(推荐post)

  get 提交方式:method=“get”,地址栏、超链接(<a href="xx">)请求方式 默认都为get

1.在地址栏显示请求信息(但容量有限 只有4-5KB)

2.出现乱码时:修改server.xml 更改tomcat的get编码 在post端口后加URIEncoding=“utf-8”

  post 提交方式:文件上传操作必须是此方式

1 出现乱码时:  request.setCharacterEncoding("utf-8");

=====================================================================================

request的常见方法:

  1 request.getParameter(String str);      按str作为key返回对应value

  2 request.getParameterValues(String str);  按str作为key返回对应多个value 用数组存储(例如checkbox按钮)

     3  request.setCharacterEncoding("utf-8");  设置请求编码

  4 request.getRequestDispatcher("b.jsp").forward(request,response); 请求转发  a-》b(页面跳转的一种方式)

  5 request.getServerContext();            获取项目的ServletContext对象

以下是对上述方法的综合示例(实现注册与显示页面)

注册页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
    <form action="show.jsp">
        用户名:<input type="text" name="uname"/><br/>
        密码:   <input type="password" name="upwd"/><br/>
        年龄:   <input type="text" name="uage"/><br/>
        爱好: <br/>
        <input type="checkbox" name="uhobbies" value="足球"/>足球    
        <input type="checkbox" name="uhobbies" value="篮球"/>篮球
        <input type="checkbox" name="uhobbies" value="乒乓球"/>乒乓球<br/>
        <input type="submit" value="注册">
    </form>
</body>
</html>

显示页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
    <%
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("uname");
        int age=Integer.parseInt( request.getParameter("uage"));
        String pwd=request.getParameter("upwd");    
        String[] hobbies=request.getParameterValues("unhobbies");
    %>
    register complete!info as follow:
    姓名:<%=name %><br/>
    年龄:<%=age %><br/>
    爱好:<br/>
    <%
        if(hobbies!=null){
            for(String hobby:hobbies)
                out.print(hobby);
        }
    %>
</body>
</html>

 response的常见方法:

1 response.addCookie(Cookie cookie)服务端向客户端增加Cookie对象

2 response.sendRedirect(String location)  重定向 (页面跳转的一种方式)

3 response.setContentType(String type):设置服务端响应编码(设置服务端的ContentType类型)

 =====================================================================================================

两种页面跳转的方式

1.response.sendRedirect(String location)               重定向

2.request.getRequestDispatcher("b.jsp").forward(request,response);  请求转发

区别示意图:

Cookie(【key-value】不是内置对象):

Cookie 是由服务端生成的,在发送给客户端保存

作用:提高访问效率,但安全性较差

常用方法:

Cookie(String name,String value)  构造方法需new

String getName()          得到name

String getValue()          得到value

setMaxAge(int time)        最大有效期(秒)

猜你喜欢

转载自www.cnblogs.com/cc123nice/p/10691961.html