1,创建HTML文件,来获取用户输入的数据:

 

1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2  <html>
3 <head>
4 <title>用户注册</title>
5
6 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
7 <meta http-equiv="description" content="this is my page">
8 <meta http-equiv="content-type" content="text/html; charset=GBK">
9
10 <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
11
12 </head>
13
14 <body>
15 <img src="qq1.jpg">
16 <table>
17 <tr>
18 <td>
19 <img src="sign.JPG">
20 </td>
21 <td>
22 <b >会员注册</b> <br><br>
23
24 <form action="index.jsp" name="form1" method="post">
25 用户姓名
26 <input type="text" name="username">
27 <br>
28 &nbsp;
29 <br>
30 填写密码
31 <input type="password" name="pass">
32 <br>
33 &nbsp;
34 <br>
35 性别
36 <input type="radio" checked="checked" name="sex" value="男">
37 <input type="radio" name="sex" value="女">
38 <br>
39 &nbsp;
40 <br>
41 爱好
42 <input type="checkbox" name="love" value="swimming">游泳
43 <input type="checkbox" name="love" value="basketball">打球
44 <br>
45 &nbsp;
46 <br>
47 <input type="submit" name="submit" value="确认提交">
48 </form>
49 </td>
50 </tr>
51 </table>
52 </body>
53  </html>
54  

 

2,编写JSP页面,接受HTML页面中表单数据,显示到浏览器上:

 

1 <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
2  <%
3  String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title>My JSP 'index.jsp' starting page</title>
13 <meta http-equiv="pragma" content="no-cache">
14 <meta http-equiv="cache-control" content="no-cache">
15 <meta http-equiv="expires" content="0">
16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17 <meta http-equiv="description" content="This is my page">
18 <!--
19 <link rel="stylesheet" type="text/css" href="styles.css">
20 -->
21 </head>
22
23 <body>
24 <%
25 String name=request.getParameter("username");
26 String Newname=new String(name.getBytes("ISO-8859-1"),"GBK");
27 out.print(Newname);
28 out.print("<br>");
29 String sex=request.getParameter("sex");
30 String Newsex=new String(sex.getBytes("ISO-8859-1"),"GBK");
31 out.print(Newsex);
32 out.print("<br>");
33 String[] love=request.getParameterValues("love");
34 for(int i=0;i<love.length;i++) {
35 out.print(love[i]+" ");
36 }
37
38
39 %>
40 <br>
41 </body>
42 </html>
43

3,配置浏览器入口显示页面,将默认的"index.jsp"替换为我们编写的HTML页面

 

1 <?xml version="1.0" encoding="GBK"?>
2 <web-app version="2.4"
3 xmlns="http://java.sun.com/xml/ns/j2ee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
6 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
7 <welcome-file-list>
8 <welcome-file>MyHtml.html</welcome-file>
9 </welcome-file-list>
10 </web-app>
11

 

当我们完成好以上步骤后,配置好Tomcat,加载项目后即可在浏览器上试验结果!

这里我们要特别注意编码问题。这里默认编码为"GBK"。

posted on 2010-07-18 00:25  KuSiuloong  阅读(13501)  评论(1编辑  收藏  举报