两个页面,一个处理页面(不显示):login,welcom,verify。
login,welcom,verify的java代码,web.xml 和pom.xml 代码如下:
package com.busymonkey;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ServletDemo
*/
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
public Login() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
PrintWriter pw = response.getWriter();
pw.println("");
pw.println("");
pw.println("Login
");
pw.println("");
pw.println("");
pw.println("");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
package com.busymonkey;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.*;
public class WelCome extends HttpServlet {
private static final long serialVersionUID = 1L;
public WelCome() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
PrintWriter pw = response.getWriter();
pw.println("Welcome!!!!");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
package com.busymonkey;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.*;
public class Verify extends HttpServlet {
private static final long serialVersionUID = 1L;
public Verify() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String u=request.getParameter("username");
String p=request.getParameter("passwd");
if (u.equals("hou") && p.equals("123")) {
response.sendRedirect("welcome");
}
else {
response.sendRedirect("login");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
Archetype Created Web Application
Login
com.busymonkey.Login
Login
/login
Verify
com.busymonkey.Verify
Verify
/verify
WelCome
com.busymonkey.WelCome
WelCome
/welcome
4.0.0
com.busymonkey
mavenWebServlet
war
0.0.1-SNAPSHOT
mavenWebServlet Maven Webapp
http://maven.apache.org
junit
junit
3.8.1
test
javax.servlet
servlet-api
2.5
provided
mavenWebServlet
以下是通过sendRedirect方法在两个页面中(这里是verify和welcome)传递数据,新代码如下,分别为verify和welcome:
package com.busymonkey;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.*;
public class Verify extends HttpServlet {
private static final long serialVersionUID = 1L;
public Verify() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String u=request.getParameter("username");
String p=request.getParameter("passwd");
if (u.equals("hou") && p.equals("123")) {
response.sendRedirect("welcome?uname=" + u);
}
else {
response.sendRedirect("login");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
package com.busymonkey;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.*;
public class WelCome extends HttpServlet {
private static final long serialVersionUID = 1L;
public WelCome() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String u = request.getParameter("uname");
try {
PrintWriter pw = response.getWriter();
pw.println("Welcome!!!! " + u);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
如果要传递两个值,则修改如下,verify和welcome:
response.sendRedirect("welcome?uname=" + u + "&upass=" + p);
String u = request.getParameter("uname");
String p = request.getParameter("upass");
try {
PrintWriter pw = response.getWriter();
pw.println("Welcome!!!! " + u + " pass=" + p);
session技术:
1:网上商城的购物车。
2:保存登陆用户的信息
3:将某些数据放入到session中,(这样也会占用服务器内存,尽量少用)
4:防止用户非法登陆到某个界面
以下是使用session技术防止非法访问页面,verify和welcome:
package com.busymonkey;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.*;
public class Verify extends HttpServlet {
private static final long serialVersionUID = 1L;
public Verify() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String u=request.getParameter("username");
String p=request.getParameter("passwd");
if (u.equals("hou") && p.equals("123")) {
HttpSession hs = request.getSession(true);
hs.setMaxInactiveInterval(20);//参数20单位秒,用于保存session的时间,20秒内无操作,刷新页面后相当于非法登录,返回登陆界面
hs.setAttribute("pass", "ok");//相当于在20秒(无操作内)赋予pass对象一个ok的值,可以在其他页面访问这个对象的值
response.sendRedirect("welcome?uname=" + u + "&upass=" + p);//传递两个以上参数用&符号
}
else {
response.sendRedirect("login");
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
package com.busymonkey;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.*;
public class WelCome extends HttpServlet {
private static final long serialVersionUID = 1L;
public WelCome() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession hs = request.getSession(true);
String val = (String) hs.getAttribute("pass");//非法登陆返回空
if ( val == null )
{
try {
response.sendRedirect("login");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
String u = request.getParameter("uname");
String p = request.getParameter("upass");
try {
PrintWriter pw = response.getWriter();//如果pass有值,则可以继续进行用户名和密码的显示
pw.println("Welcome!!!! " + u + " pass=" + p);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
1、HttpSession hs = request.getSession(true); //得到和request相关的session,如果没有就创建。
2、String sessionId = hs.getId(); //得到session的Id,这是由容器分配的。
3、String val = (String) hs.getAttribute("pass"); //通过名称name从session中得到数据。
4、hs.removeAttribute( String name); //删除一个属性。