将对象从一个servlet传递到另一个servlet

时间:2014-08-08 07:40:13

标签: html servlets

我有一个包含三个链接的HTML表单(index.html),这些链接是登录,注销和配置文件。单击登录链接后,将显示一个HTML页面,其中包含两个名为username和password的字段。输入正确的用户名和密码后,我的servlet1会响应一条消息,例如" welcome user"。我的注销链接与servlet2相关联,该servlet2删除当前请求的cookie并显示消息"成功注销"点击时。我想知道我的index.html文件中与三个链接(登录,注销,配置文件)相关的所有请求和响应对象 用户会一样吗?我还在这里提到我的代码: login.java:

package loginandlogout;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 public class Login extends HttpServlet
 {
     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
     {
         response.setContentType("text/html");
         PrintWriter out= response.getWriter();

         request.getRequestDispatcher("link.html").include(request,response);
         String name=request.getParameter("username");
         String password=request.getParameter("password");
         if(password.equals("admin"))
         {
             out.println("you are successfully login...");
             out.println("welcome "+name);
             Cookie ck= new Cookie("name",name);
             response.addCookie(ck);
         }
         else
         {
             out.println("Not authorized");
             request.getRequestDispatcher("login.html").include(request, response);
         }
     }
 }

logout.java:

package loginandlogout;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Logout extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        int j=0;
        response.setContentType("text/html");
        PrintWriter out= response.getWriter();
        Cookie ck[]=request.getCookies();

        if(ck==null||ck[0].equals(""))
        {
        request.getRequestDispatcher("link.html").include(request, response);
            out.println("without login how can logout you..");
        }
        else
        { 
            int i;
            int size1=ck.length;
            for(i=0; i<size1; i++)
            {
                String ckname=ck[i].getValue();
                if(ckname.equals(request.getParameter("username")))
                {
                    out.println("successfully logout when i am in loop");
                    j=i;
                    break;
                }
            }

            ck[j]=new Cookie("name","");
            ck[j].setMaxAge(0);
            response.addCookie(ck[j]);
            request.getRequestDispatcher("link.html").include(request, response);
            out.println("successfully logout " +ck[j].getValue());
        }
    }
}

profile.java

package loginandlogout;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Profile extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        response.setContentType("text/html");
        PrintWriter out= response.getWriter();

        request.getRequestDispatcher("link.html").include(request, response);
        Cookie ck[]=request.getCookies();
        if(ck !=null)
        {
            String name= ck[0].getValue();
            if(!name.equals("")|| name!=null)
            {
                out.println("welcome "+name);
            }
            else
                out.println("please loing first");
        }
        else
        {
            out.println("please loing first");
        }

    }
}

的index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>home page</title>
</head>
<body>

    <h1>welcome to my home page...</h1>
    <br />
    <a href="login.html">login</a>|
    <a href="logout">logout</a>|
    <a href="profile">profile</a>
</body>
</html>

0 个答案:

没有答案