JSP表单处理

2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="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 '2.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>
      <h2>接收从index.jsp发送过来的数据</h2>
       <!-- 
          GET方法提交时,中文乱码的解决方案有2种:
          1).对获取的参数数据进行转换,转换为byte数组,再把byte数组重新编码为UTF-8格式的字符串
          2).修改tomcat服务器目录下的conf目录里的server.xml文件,在Connector节点那里添加
          属性 URIEncoding="UTF-8",重启服务器就可以了
        -->
        <%
           String  sitename=request.getParameter("sitename");
             /*    byte[] bt= sitename.getBytes("ISO8859-1");
                sitename=new String(bt,"UTF-8");
                */
        %>
    网站名:  <%=sitename %><br>
  网址:<%=request.getParameter("url") %>
  </body>
</html>

3.jsp

<%@ page language="java" import="java.util.*" pageEncoding="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 '2.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>
      <h2>接收从index.jsp发送过来的数据</h2>
       <!-- 
          POST方法提交时,中文乱码的解决方案:
          在第1句获取表单参数的前面加上request.setCharacterEncoding("UTF-8") 
        -->
        <%
         request.setCharacterEncoding("UTF-8");
         String  sitename=request.getParameter("sitename");
         String url=request.getParameter("url");
         String[] citys=  request.getParameterValues("city");
         for(String s:citys){
        	%>
        	 城市:<%=s %><br>
        <%
         }
        %>
    网站名:  <%=sitename %><br>
  网址:<%=url %>
  </body>
</html>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="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 'index.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>
      <h2>网站表单--GET方法提交</h2>
    <form action="2.jsp">
       网站名:<input type="text" name="sitename"><br>
        网址:<input type="text" name="url"><br>
        <input type="submit" value="GET提交">
    </form>
       <h2>网站表单--POST方法提交</h2>
    <form action="3.jsp"  method="post">
       网站名:<input type="text" name="sitename"><br>
        网址:<input type="text" name="url"><br>
   校区城市:<input type="checkbox" name="city" value="beijing">北京
   <input type="checkbox" name="city" value="shanghai">上海
   <input type="checkbox" name="city" value="wuhan">武汉
        <input type="submit" value="POST提交">
        
    </form>
  </body>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值