4.2
获取信息
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>获取FORM表单信息</title>
</head>
<body>
<form action="output.jsp" method="post">
<table border=1>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="radio" name="sex" value="男" >男
<input type="radio" name="sex" value="女">女
</td>
</tr>
<tr>
<td>爱好</td>
<td>
<input type="checkbox" name="hobby" value="篮球" >篮球
<input type="checkbox" name="hobby" value="旅游">旅游
<input type="checkbox" name="hobby" value="音乐">音乐
<input type="checkbox" name="hobby" value="摄影">摄影
</td>
</tr>
<tr>
<td>E-mail:</td>
<td style="width: 350px"><input type="text" name="E-mail"/></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit"/>
<input type="reset" value="全部重写">
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户输入信息</title>
</head>
<body>
<form>
<table border="1">
<tr>
<td>姓名:</td>
<td style="width: 350px">
<%=new String(request.getParameter("name").getBytes("ISO_8859_1"),"UTF-8")%>
</td>
</tr>
<tr>
<td>性别:</td>
<td>
<%=new String(request.getParameter("sex").getBytes("ISO_8859_1"),"UTF-8")%>
</td>
</tr>
<tr>
<td>爱好:</td>
<td>
<%
String[] like =request.getParameterValues("hobby");
for (int i = 0; i < like.length; i++){
%>
<%=new String(like[i].getBytes("ISO_8859_1"),"UTF-8")+" "%>
<%}%>
</td>
</tr>
<tr>
<td>E-mail:</td>
<td>
<%=new String(request.getParameter("E-mail"))%>
</td>
</tr>
</table>
</form>
</body>
</html>
4.5
登录验证
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%
String str = request.getParameter("username");
String pwd = request.getParameter("pwd");
if(str!=null){
if(str.equals("tom") && pwd.equals("123")){
out.println("您好,tom!");
}
else{
out.println("您的账号密码有误,请重新输入。");
}
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content ="text/html;charset=UTF-8">
<title>Work2</title>
</head>
<body>
<form action="work1.jsp" method="post">
账号:<input type="text" name="username"/><br/>
密码:<input type="password" name="pwd"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
work1.jsp
<%
String str = request.getParameter("username");
String pwd = request.getParameter("pwd");
String username = str;
if(str!=null) {
if (str.equals("tom") && pwd.equals("123")) {
out.println("您好,tom!");
} else {
out.println("您的账号密码有误,请重新输入。");
}
response.sendRedirect("work2.jsp");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content ="text/html;charset=UTF-8">
<title>Work1</title>
</head>
<body>
<form action="work1.jsp" method="post">
账号:<input type="text" name="username"/><br/>
<%String name = request.getParameter("username");
session.setAttribute("session_name",name);
%>
密码:<input type="password" name="pwd"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
work2.jsp
<%String name1 = (String) session.getAttribute("session_name");
if (name1!=null){
out.println("欢迎您"+name1);
}
else{
out.println("欢迎您,"+name1+"非正常访问");
}
%>
<html>
<head>
<title>title</title>
</head>
<body>
</body>
</html>
经过登录页面是,会跳转到
当不经过登录页面是