javabean 使用form表单

formbean.html

<!DOCTYPE html>
<html>
  <head>
    <title>formbean.html</title>
	
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
  <form action="formbean.jsp" method = "post">
  姓名:
  <input type = "text" name = "name">
 性别:
 <select name = "sex">
 <option value = "先生">先生</option>
 <option value = "小姐">小姐</option>
 </select>
 年龄:
 <input type = "text" name = "age">
  <input type = "submit" name = "submit">

  </form>
  </body>
</html>

formbean.jsp

<%@ page contentType="text/html; charset=gbk" language="java" import="test.formbean" errorPage="" %>
<%
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%>">
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    <title>formbean</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>




<jsp:useBean id="formbean" class = "test.formbean"></jsp:useBean>
<jsp:setProperty property= "*"  name="formbean" />


显示用户信息
<jsp:getProperty property="name" name="formbean"/>
<jsp:getProperty property="age" name="formbean"/>
<jsp:getProperty property="sex" name="formbean"/>



</body>
</html>

formbean.java

package test;

public class formbean {
	private String name = "";
	private String sex = "";
	private String age = "";

	public void setName(String name)

	{
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setSex(String sex) {
		this.sex = ConvertGBK(sex);
	}

	public String getSex() {
		return this.sex;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getAge() {
		return this.age;
	}
	
	//汉字转换方法
	 public String ConvertGBK(String str){
	          String strReturn="";
	          try{
	           strReturn=new String(str.getBytes("ISO-8859-1"),"GBK");
	          }catch(Exception ex){
	           System.out.println("TestBean.ConvertGBK():ex="+ex.toString());
	          }
	          finally{
	           return strReturn;
	          }
	   }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注