Jsp之常用的内置对象

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/woainijinying/article/details/52835995
Request对象主要用于处理客户端请求(内置对象,无需请求)
请求:get post请求
常用方法:

下面:我们来写一个简单的传值小程序,一个小表单,在另一个Jsp里将表单的信息输出
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'table.jsp' starting page</title>
    <meta charset="utf-8">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
  <script type="text/javascript">
 
  $(function(){
     $("#bt1").click(function(){
    
      $("form").submit();
     });
   });
</script>
  </head>
  
  <body>
  <form action="go.jsp" method="post">
  <table>
  <tr>
  <td>用户名:</td>
  <td><input type="text" name="name"></td>
  </tr>
  <tr>
  <td>密码:</td>
  <td><input type="text" name="password"></td>
  </tr>
   <tr>
  <td>确认密码:</td>
  <td> <input type="text" name="again"></td>
  </tr>
   <tr>
  <td> 性别:</td>
  <td>
  <input type="radio" name="sex" value="男" checked>男
  <input type="radio" name="sex" value="女">女
  </td>
  
  </tr>
  <tr>
  <td> 电子邮箱地址:</td>
  <td><input type="text" name="email"></td>
  </tr>
  <tr>
  <td> 兴趣爱好:</td>
  <td>
  <input type="checkbox" name="hobby" value="体育" checked>体育
  <input type="checkbox" name="hobby" value="读书">读书
  <input type="checkbox" name="hobby" value="音乐">音乐
  <input type="checkbox" name="hobby" value="旅游">旅游
  </td>
  </tr>
  </table>
  <input type="button" value="同意以下条款" id="bt1">
   <input type="color">
   <input type="date">
  </form>     
  </body>
</html>
效果图:

显示程序:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=utf-8" %>
<%
request.setCharacterEncoding("utf-8");
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String name=request.getParameter("name");
String password=request.getParameter("password");
String again=request.getParameter("again");
String sex=request.getParameter("sex");
String email=request.getParameter("email");
String hobby[]=request.getParameterValues("hobby");

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'go.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <p>用户名:<%=name%></p>
    <p>密码:<%=password%></p>
    <p>确认密码:<%=again%></p>
    <p>性别:<%=sex%></p>
    <p>邮箱:<%=email%></p>
    <p>爱好:
    
    <%for(int i=0;i<hobby.length;i++){ %>
    <%=hobby[i]%> 
    <%} %>
    </p>
  </body>
</html>
效果图:

需要注意的是:
中文需要改:tomcat中的conf/server.xml中添加
useBodyEncodingForURI="true" URIEncoding="UTF-8"
页面传值:get 方式 通过连接提交
至此,简单的内置对象就结束啦,啦啦啦









猜你喜欢

转载自blog.csdn.net/woainijinying/article/details/52835995