服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

新手入门:jsp基本语法与简单表单处理

jsp语法:

jsp指令元素

(1)include:导入其它文件夹

(2)page:

language:用什么语言,只能为java

contenttype:mime类型

import:导入java包

(3)taglib:自定义标签库

jsp常用标准元素

(1)jsp:forward:跳转到其它页面

(2)jsp:include:插入其它文件 eg:

(3)jsp:plugin:插入applet小程序

(4)jsp:param:参数传值

jsp内置对象

(1)request:常用方法

getparameter():提取表单元素的值

getremoteaddr():获取客户端ip值

(2)response:

sendredirect():重定向到其它网页

setcontenttype();设置mime值

(3)out:向网页输出

(4)application

setattribute(string,object)把变量的值保存在application中;

getattribute(string)获取保存在applicaion中的值

removeattribute(string)删除保存在application中的值

(5)session

setattribute(string,object)把变量的值保存在session中;

getattribute(string)获取保存在session中的值

removeattribute(string)删除保存在

getid:获取session编号

jsp简单表单处理

<%@page contenttype="text/html;charset=gb2312"%>
<%@page language="java" %>
<html>
<head><title>表单处理</title></head> 
<form name="frm" method="get" action="ch-show.jsp">
<table boder=0>
<tr><td>用户名:</td><td><input type=text name="tname"></td></tr>
<tr><td>密码:</td><td><input type=password name="tpass"></td></tr>
<tr><td>性别:</td>
<td><input type=radio name="tsex" value="男" checked>男
<input type=radio name="tsex" value="女">女</td>
</tr>
<tr><td>爱好:</td>
<td><input type=checkbox name=tch1 value="体育">体育
<input type=checkbox name=tch2 value="美术">美术
<input type=checkbox name=tch3 value="音乐">音乐</td>
</tr>
<tr><td>专业:</td><td><select name=ty>
<option value="计算机">计算机</option>
<option value="文学">文学</option>
<option value="数学">数学</option>
</select>
<tr><td>留言:</td><td><textarea name=tl rows=5 cols=20></textarea></td></tr>
<tr><td><input type=submit value="用户信息"></td></tr>
</table></form>
 <%
string tname=request.getparameter("tname");
string tpass=request.getparameter("tpass");
string tsex=request.getparameter("tsex");
string tlove1=request.getparameter("tch1");
string tlove2=request.getparameter("tch2");
string tlove3=request.getparameter("tch3");
string ty=request.getparameter("ty");
string tl=request.getparameter("tl");
 byte b1[]=tsex.getbytes("iso-8859-1");
tsex=new string(b1);
if(tlove1==null)
{
tlove1="";
}
else
{
byte b2[]=tlove1.getbytes("iso-8859-1");
tlove1=new string(b2);
}
if(ty==null)
{
ty="";
}
else
{
byte b5[]=ty.getbytes("iso-8859-1");
ty=new string(b5);
}
if(tlove2==null)
{
tlove2="";
}
else
{
byte b3[]=tlove2.getbytes("iso-8859-1");
tlove2=new string(b3);
}
if(tlove3==null)
{
tlove3="";
}
else
{
byte b4[]=tlove3.getbytes("iso-8859-1");
tlove3=new string(b4);
}
  out.print("你的信息是:"+"<br>");
out.print("用户名"+tname+"<br>");
out.print("密码"+tname+"<br>");
out.print("性别"+tsex+"<br>");
out.print("爱好"+tlove1+tlove2+tlove3+"<br>");
out.print("专业"+ty+"<br>");
out.print("留言"+tl+"<br>");
%>
</body>

扫描关注微信公众号