核心代码:
index.jsp
<body>
<form action="request.jsp">
username: <input type="text" name="username"><br>
password: <input type="password" name="password"><br>
<input type="submit" value="提交" name="submit">
<input type="reset" value="reset">
</form>
</body>
request.jsp
<body>
<!--获取表单信息 -->
<% String struser = request.getParameter("username"); %>
<% String strpass = request.getParameter("password"); %>
<%=struser %><br/>
<%=strpass %><br/>
<%
if(struser==null)
struser="";
if(strpass==null)
strpass="";
if(struser.equals("xue") && strpass.equals("123"))
out.println("登录成功");
else
out.println("登录失败");
%>
<!--处理中文输出乱码 -->
<% String btnName = request.getParameter("submit");
/* byte c[] = btnName.getBytes("UTF-8");
btnName = new String(c); */
%>
<%=btnName %>
<br/>
<!--request常用方法 -->
<%= request.getProtocol() %><br/>
<%= request.getScheme() %><br/>
<%= request.getServerName() %><br/>
<%= request.getServerPort() %><br/>
<%= request.getRemoteAddr() %><br/>
<%= request.getRemoteHost() %><br/>
<!--response对象,动态改变ContentType属性 -->
<form action="" method="get" name="form">
<input type="submit" value="yes" name="submit1"/>
</form>
<%
String str = request.getParameter("submit1");
if(str==null)
str="";
//默认保存当前页面
if(str.equals("yes"))
response.setContentType("application/msword;charset=GB2312");
%>
<br/>
<!--重定向,注意只有表单提交之后才能用request对象 -->
<form action="">
<input type="submit" value="redirect" name="redirect"/>
</form>
<%
String redirect = request.getParameter("redirect");
if(redirect==null)
redirect="";
if(redirect.equals("redirect"))
response.sendRedirect("session.jsp");
%>
</body>