深入体验JavaWeb开发内幕——一个获取form表单中的相关组件值的应用

Register.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Register.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
  <form action ="RequestLogin" method ="get">
    用户名:<input type="text" name = "username" ><br/>
    密码:  <input type="password" name = "password"><br/>
  性别:<input type="radio" name = "sex" value ="male">男
      <input type="radio" name = "sex" value ="female">女<br/>
  籍贯: <select name ="city">
        <option value ="HeBei">河北</option>
        <opton value ="HuBei">湖北</opton>
        <option value ="ShanXi">山西</option>
      </select><br/>
简历:<br/>
   &nsp;<textarea rows="5" cols="20" name ="intro"></textarea>
<br/>
爱好:<br/>
<input type="checkbox" name ="hobbies" value ="sing"/>唱歌
<input type="checkbox" name ="hobbies" value ="dance"/>跳舞
<input type="checkbox" name ="hobbies" value ="readbook"/>读书
<input type="checkbox" name ="hobbies" value ="readnewspaper"/>看报<br/>
上传头像:<br/>
<input type="file" value ="image" name ="browser"><br/>
<input type="submit" value ="提交"/>
  </form>
  <a href ="/Request/RequestLogin?time=时间就是金钱">点击这里</a>
  
  </body>
</html>

RegisterLogin.java

 package net.csdn.request;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestLogin extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 getInformation(request);

		   
	}

	

	private void getInformation(HttpServletRequest request)
			throws UnsupportedEncodingException {
		//只对post请求才起作用
//		  request.setCharacterEncoding("utf-8");
		//当请求方式为get时
		   String name = request.getParameter("username");
		   String pass = request.getParameter("password");
		   String sex = request.getParameter("sex");
		   String city = request.getParameter("city");
		   String intro = request.getParameter("intro");
		   String  [] hobbies = request.getParameterValues("hobbies");
		   String hobby ="";
		   //hobbies!=null对所取值为空时进行设置
		   for(int i=0;hobbies!=null&&i<hobbies.length;i++){
			  String  hovalue = hobbies[i];
			  hobby += hovalue;
		   }
		   //获取头像信息
		  // String image = request.getParameter("image");
		  
		 String username = new String(name.getBytes("iso8859-1"),"utf-8");
		 String introduction = new String(intro.getBytes("iso8859-1"),"utf-8");
		
		   System.out.println("username:"+username);
		   System.out.println("password:"+pass);
		   System.out.println("sex:"+sex);
		   System.out.println("city:"+city);
		   System.out.println("intro:"+introduction);
		   System.out.println("hobby:"+hobby);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值