4实现数据保存
1.使用session保存用户名
if(username.equals("admin")){
//不允许注册,注册失败
request.setAttribute("mess", "注册失败,请更换其他用户名");
request.getRequestDispatcher("userCreate.jsp").forward(request, response);
}else{
//允许注册,注册成功
session.setAttribute("user", username);
response.sendRedirect("index.jsp");
}
<%
Object o=session.getAttribute("user");
if(o==null){
//显示用户名密码,可以在此登录
%>
<label>用户名</label><input type="text" name="uname" /><label>密码</label><input type="text" name="upassword" /><button>登录</button>
<%
}else{
//显示“欢迎你,XXX”
out.print("欢迎你,"+o.toString());
}
%>
2.会话清除与过期
if(username.equals("admin")){
//不允许注册,注册失败
request.setAttribute("mess", "注册失败,请更换其他用户名");
request.getRequestDispatcher("userCreate.jsp").forward(request, response);
}else{
//允许注册,注册成功
session.setAttribute("user", username);
//session.setMaxInactiveInterval(10);
response.sendRedirect("index.jsp");
}
①
<%
//实现注销
//session.removeAttribute("user");
session.invalidate();
response.sendRedirect("index.jsp");
%>
②
<session-config>
<session-timeout>10</session-timeout>
</session-config>
</web-app>
3使用cookie自动填写用户名
if(username.equals("admin")){
//不允许注册,注册失败
request.setAttribute("mess", "注册失败,请更换其他用户名");
request.getRequestDispatcher("userCreate.jsp").forward(request, response);
}else{
//允许注册,注册成功
Cookie cookie=new Cookie("user",username);
cookie.setMaxAge(60*60);
response.addCookie(cookie);
session.setAttribute("user", username);
//session.setMaxInactiveInterval(10);
response.sendRedirect("index.jsp");
}
<%
Cookie[] cookies=request.getCookies();
String user="";
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals("user")){
user=cookies[i].getValue();
}
}
Object o=session.getAttribute("user");
if(o==null){
//显示用户名密码,可以在此登录
%>
4application实现计数器
<%
Object count=application.getAttribute("count");
if(count==null){
//application中未存放count
application.setAttribute("count", new Integer(1));
}else{
//application中已经存放count
Integer i=(Integer)count;
application.setAttribute("count", i.intValue()+1);
}
Integer icount=(Integer)application.getAttribute("count");
out.println("页面被访问了"+icount.intValue()+"次");
%>
5jsp总结
1.客户端请求新页面一(超链接和js请求,get方式传参)
<script type="text/javascript">
function fun(){
var uid = document.getElementById("uid").value;
if(uid == ""){
alert("请输入数据");
return false;
}else{
return true;
}
}
<!-- 链接到page2 -->
<a href="page2.jsp">链接到page2</a><br>
<!-- 链接到page2,弹出新窗口 -->
<a href="page2.jsp" target="_blank">链接到page2</a><br>
<!-- 相对路径链接到page2 -->
<a href="./page2.jsp">链接到page2</a><br>
<a href="../demo1/page2.jsp">链接到page2</a><br>
<!-- 绝对路径链接到page2 -->
<a href="http://localhost:8080/web1/demo1/page2.jsp">链接到page2</a><br>
<a href="<%=request.getContextPath() %>/demo1/page2.jsp">链接到page2</a><br>
<!-- 链接到page2,并传参 -->
<a href="page2.jsp?uid=admin">链接到page2</a><br>
<!-- 使用js链接到page2 -->
<a href='javascript:window.location="page2.jsp";'>链接到page2</a><br>
<!-- 使用js链接到page2,并传参 -->
<a href='javascript:window.location="page2.jsp?uid=admin";'>链接到page2</a><br>
<!-- 使用js链接到page2,弹出新窗口,并传参 -->
<a href='javascript:window.open("page2.jsp?uid=admin");'>链接到page2</a><br>
<!-- submit提交表单 -->
<form action="page2.jsp" method="post">
<input type="text" name="uid">
<input type="submit" value="提交">
</form>
<!-- 验证后提交表单 -->
<form action="page2.jsp" method="post">
<input type="text" name="uid" id="uid">
<input type="submit" value="验证后提交表单" onClick="return fun()">
</form>
<!-- url方式提交数据 -->
<form action="page2.jsp?uid=admin" method="post">
<input type="text" name="uid">
<input type="submit" value="提交">
</form>
<!-- hidden方式提交数据 -->
<form action="page2.jsp" method="post">
<input type="hidden" name="uid2" id="uid2" value="admin">
<input type="submit" value="提交">
<script type="text/javascript">
document.getElementById("uid2").value = "admin22222";
</script>
</form>
<!-- button+js提交表单 -->
<form id="form1" action="page2.jsp" method="post">
<input type="text" name="uid">
<input type="button" value="提交" οnclick='document.getElementById("form1").submit();'>
</form>
<!-- <a>+js提交表单 -->
<form id="form2" action="page2.jsp" method="post">
<input type="text" name="uid">
<a href='javascript:document.getElementById("form2").submit();'>提交表单</a>
</form>
2.客户端请求新页面二
3.中文乱码解决方法
cookie中文转码解码
<%
Cookie c = new Cookie("mess",URLEncoder.encode("中国") );
response.addCookie(c);
%>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
//name = new String(name.getBytes("ISO-8859-1"),"utf-8");
out.print("get方式提交的name的值"+name+"<br>");
String uid = request.getParameter("uid");
//uid = new String(uid.getBytes("ISO-8859-1"),"utf-8");
out.print("post方式提交的uid的值"+uid+"<br>");
%>
<%
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(Cookie c : cookies){
if(c.getName().equals("mess")){
out.print("Cookie的值为"+URLDecoder.decode(c.getValue()) );
}
}
}
%>