java servlet会话维护

时间:2013-03-06 13:48:49

标签: servlets httpsession

我开发了用于生成Captcha代码的servlet&添加会话名称 代码

生成Capta代码

import javax.servlet.*;
import java.io.*;
import java.awt.*;
import java.util.Random;
import java.awt.image.*;
import javax.imageio.*;
import javax.servlet.http.*;
public class capta extends HttpServlet
{

    final int LEN=10;
    BufferedImage image;
    Random rnd;
    public void doGet(HttpServletRequest req,HttpServletResponse rep)
    {
        try
        {
            image = new BufferedImage(200, 80, BufferedImage.TYPE_INT_RGB); 
            rnd=new Random();
            rep.setContentType("image/jpeg");
            String random_String=getRandomNumbers();
            HttpSession ses=req.getSession();
            ses.setAttribute("code",random_String);
            Graphics g = image.getGraphics();
            g.setFont(new Font("Maiandra GD",1,30));
            g.setColor(Color.blue); 
            g.drawString(random_String,3,50);
            g.dispose();
            /// it was here
            ImageIO.write(image, "jpg", rep.getOutputStream());
        }
        catch(Exception e)
        {

        }
    }
    private String getRandomNumbers()
    {
        StringBuffer str=new StringBuffer();
        for(int i=1;i<=9;i++)
        {
            str.append(new Integer(rnd.nextInt(9)).toString()); 
        }
        return str.toString();
    }

    public void doPost(HttpServletRequest req,HttpServletResponse rep)throws ServletException , IOException
    {
        doGet(req,rep);
    }

}

使用在其他servlet中生成capta-image

import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
public class capta_test extends HttpServlet
{


    public void doGet(HttpServletRequest req,HttpServletResponse rep)
    {
        PrintWriter out=null;
        try
        {
            String input=req.getParameter("txt_capta");
            out=rep.getWriter();            
            rep.setContentType("text/html");
            out.println("<html><head><title>Capta Test</title></head>");    
            out.println("<body>");  
            out.println("<h1> Capta  is Generated </h1>");

            out.println("<img src=\"http://localhost:8080/mahesh/capta\" />");
            HttpSession ses=req.getSession();

            String original=(String)ses.getAttribute("code");
            if(original!=null)
            {
                out.println("<h1>Original Value "+original+"</h1>");
            }
            ses.invalidate();
            out.println("</body>");     
            out.println("</html>"); 
        }
        catch(Exception e)
        {

        }
    }

    public void doPost(HttpServletRequest req,HttpServletResponse rep)throws ServletException , IOException
    {
        doGet(req,rep);
    }

}

两个servlet工作正常,但我没有得到在Capta-code servelt中生成的会话值(在servlet中添加)
我得到的会话值是在之前的capta-image中生成的。

见输出:
输出 - 1

Output - 1

输出 - 2

enter image description here

输出 - 3

enter image description here

谢谢..

2 个答案:

答案 0 :(得分:1)

您的程序流程错误,会话值为空,因为capta_test servlet is called even before the capta servlet

看看这个简单的教程。希望这有助于您走上正确的道路

Captcha in JSP and Servlet

答案 1 :(得分:1)

将随机数生成逻辑放在 capta_test servlet中,并将该数字放入会话中。因为您正在呈现HTML,所以在页面加载之后将调用 capta servlet,并且不会使会话无效。

随机数生成器

import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
public class capta_test extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse rep)
{
    PrintWriter out=null;
    try
    {
        String original=getRandomNumbers();
        HttpSession ses=req.getSession(false);
        ses.setAttribute("code",random_String);
        String input=req.getParameter("txt_capta");
        out=rep.getWriter();            
        rep.setContentType("text/html");
        out.println("<html><head><title>Capta Test</title></head>");    
        out.println("<body>");  
        out.println("<h1> Capta  is Generated </h1>");

        out.println("<img src=\"http://localhost:8080/mahesh/capta\" />");
        out.println("<h1>Original Value "+original+"</h1>");
        out.println("</body>");     
        out.println("</html>"); 
    }
    catch(Exception e)
    {

    }
}

public void doPost(HttpServletRequest req,HttpServletResponse rep)throws ServletException , IOException
{
    doGet(req,rep);
}
private String getRandomNumbers()
{
    Random rnd=new Random();
    StringBuffer str=new StringBuffer();
    for(int i=1;i<=9;i++)
    {
        str.append(new Integer(rnd.nextInt(9)).toString()); 
    }
    return str.toString();
}
}

图像生成器servlet

import javax.servlet.*;
import java.io.*;
import java.awt.*;
import java.util.Random;
import java.awt.image.*;
import javax.imageio.*;
import javax.servlet.http.*;
public class capta extends HttpServlet
{



  final int LEN=10;

BufferedImage image;
public void doGet(HttpServletRequest req,HttpServletResponse rep)
{
    try
    {
        HttpSession ses=req.getSession(false);

        String random_String=(String)ses.getAttribute("code");
        image = new BufferedImage(200, 80, BufferedImage.TYPE_INT_RGB); 
        rep.setContentType("image/jpeg");
        Graphics g = image.getGraphics();
        g.setFont(new Font("Maiandra GD",1,30));
        g.setColor(Color.blue); 
        g.drawString(random_String,3,50);
        g.dispose();
        /// it was here
        ImageIO.write(image, "jpg", rep.getOutputStream());
    }
    catch(Exception e)
    {

    }
}
public void doPost(HttpServletRequest req,HttpServletResponse rep)throws ServletException , IOException
{
    doGet(req,rep);
}

}