当前位置:编程学习 > JAVA >>

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

[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
[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); 
    } 
 


 

补充:Web开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,