如何在我的网络应用程序中添加cookie的代码

时间:2015-01-16 15:08:24

标签: java cookies

我是否可以在我的Web应用程序中添加任何代码以实现cookie?这是我在web.xml和jsp中可以做到的确切方式吗?我想实现用于登录的cookie,但是我不知道我怎么能这样做。可以有人给我关于如何实现它的确切说明吗?

非常感谢您的帮助和支持。

/*

*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。  *要更改此模板文件,请选择“工具”|模板  *并在编辑器中打开模板。  * /

打包my_servlets;

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

import javax.servlet.RequestDispatcher;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  

import connect_with_my_sql_database.LoginDao;  

import javax.servlet.http.Cookie;

public class Login extends HttpServlet{  

    private static final long serialVersionUID = 1L;  

    protected void doGet(HttpServletRequest request, HttpServletResponse response)    
            throws ServletException, IOException {


    }

  @Override protected void doPost(HttpServletRequest request, HttpServletResponse response)    
            throws ServletException, IOException {    

        response.setContentType("text/html");    
       PrintWriter out = response.getWriter();   

        String n=request.getParameter("j_Username");    
        String p=request.getParameter("j_Password");   

        HttpSession session = request.getSession(false);  
        if(session!=null)  {
             session.setAttribute("Username", n);  
             session.setAttribute("Password", p);  
        }

        if(LoginDao.validate(n, p)){   

            Cookie c1=new Cookie("j_Username",encrypt(n));//the username and passwrd are //encrypted
            Cookie c2=new Cookie("j_Password",encrypt(p));
            c1.setMaxAge(20000);
            c2.setMaxAge(20000);
            response.addCookie(c1);
            response.addCookie(c2);

            RequestDispatcher rd=request.getRequestDispatcher("style/success.jsp"); 
            rd.forward(request,response);    
        }    
        else{  
           //out.print("<p  style=\"color:red\">Sorry "+n+" or "+p+" error</p>");
            RequestDispatcher rd=request.getRequestDispatcher("style/loginError.jsp");    
            rd.forward(request,response);    
        }    


        out.close();    
    }    
}   

1 个答案:

答案 0 :(得分:0)

让我们假设有一个login.jsp,其中包含uname和password字段,当我们点击提交时,控件转到LoginCheck servlet。 LoginCheck将检查用户名和密码是否正确。如果是,则创建两个cookie并将其发送到浏览器。

的Login.jsp

<form action="LoginCheck" method="post">
<input type="text" name="uname">
<input type="pasword" name="password">
<input type="submit">
</form>

LoginCheck-- Servlet

public void doPost(HttpServletRequest request,HttpServletResponse response)
{
String uname=request.getParameter("uname");
String password=request.getParameter("password");
if(checkLogin(uname,password))//checkLogin going to check with database if the user is logged in
{
Cookie c1=new Cookie("uname",encrypt(uname));//the username and passwrd are //encrypted
Cookie c2=new Cookie("password',encrypt(password));
c1.setMaxAge(20000);
c2.setMaxAge(20000);
response.addCookie(c1);
response.addCookie(c2);//sends cookies to the browser
}

希望这有帮助。