servlet之间的数据传递

1:如何让用户的请求数据从一个Servlet传递给另一个Servlet

    第一种方式:通过超链接传递数据
    第二种方式:通过表传递取参数。
    第三种方式:通过setAttribute("name",value)与getAttribute(name value)传递参数

  1)通过超链接获得参数:
    需要在超链接后面给定要传递参数的值。但长度有限制

Java代码 
package testParameter;  
 
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;  
 
public class TestA extends HttpServlet{  
 
    @Override 
    protected void service(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
      
        response.setContentType("text/html");  
        PrintWriter out=response.getWriter();  
        String name="hello,everyone";  
        out.println("<html>");  
        out.println("<body>");  
        out.println("<a href='/JSP/test?id="+name+"'>this is a test</a>");  
        out.println("<body>");  
        out.println("</html>");  
                out.flush();  
          
    }  


package testParameter;

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;

public class TestA extends HttpServlet{

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

response.setContentType("text/html");
PrintWriter out=response.getWriter();
String name="hello,everyone";
out.println("<html>");
out.println("<body>");
out.println("<a href='/JSP/test?id="+name+"'>this is a test</a>");
out.println("<body>");
out.println("</html>");
                out.flush();

}
}


通过request.getParameter("id")获得参数的值:

Java代码 
package testParameter;  
 
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;  
 
public class Test extends HttpServlet {  
 
    @Override 
    protected void service(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
          
        String name=request.getParameter("id");  
        response.setContentType("text/html;charset=gb2312");  
        PrintWriter out=response.getWriter();  
          
        out.println("<html>");  
        out.println("<body>");  
        out.println(name);  
        out.println("</body>");  
        out.println("</html>");  
                out.flush();  
    }  


package testParameter;

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;

public class Test extends HttpServlet {

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

String name=request.getParameter("id");
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();

out.println("<html>");
out.println("<body>");
out.println(name);
out.println("</body>");
out.println("</html>");
                out.flush();
}
}
通过表单获得参数:
Java代码 
package testParameter;  
 
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;  
 
public class TableP extends HttpServlet{  
 
    @Override 
    protected void service(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException {  
        response.setContentType("text/html;charset=gb2312");  
        PrintWriter out=response.getWriter();  
        out.println("<html>");  
        out.println("<body>");  
        out.println("<form action='/JSP/tabletest' method='post'>");  
        out.println("<table align='center'>");  
          
        out.println("<tr>");  
        out.println("<td>");  
        out.println("username:");  
        out.println("</td>");  
        out.println("<td>");  
        out.println("<input type='text' name='name'>");  
        out.println("</td>");  
        out.println("</tr>");  
          
        out.println("<tr>");  
        out.println("<td>");  
        out.println("password:");  
        out.println("</td>");  
        out.println("<td>");  
        out.println("<input type='text' name='pwd'>");  
        out.println("</td>");  
        out.println("</tr>");  
          
        out.println("<tr >");  
        out.println("<td colspan='2'>");  
        out.println("<input type='submit' value='提交'>");  
        out.println("</td>");  
        out.println("</tr>");  
          
        out.println("</table>");  
        out.println("</form>");  
        out.println("</body>");  
        out.println("</html>");     
                out.flush();  
    }  
}  
 
package testParameter;  
 
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;  
 
public class GetTableP extends HttpServlet{  
 
    @Override 
    protected void service(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
          
        resp.setContentType("text/html;charset=gb2312");  
        PrintWriter out=resp.getWriter();  
          
        String name=req.getParameter("name");  
        String pwd=req.getParameter("pwd");  
          
        out.println("<html>");  
        out.println("<body>");  
        out.println(name);  
        out.println("<hr>");  
        out.println(pwd);  
        out.println("</body>");  
        out.println("</html>");  
                out.flush();  
          
    }  
      
      
 


package testParameter;

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;

public class TableP extends HttpServlet{

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<form action='/JSP/tabletest' method='post'>");
out.println("<table align='center'>");

out.println("<tr>");
out.println("<td>");
out.println("username:");
out.println("</td>");
out.println("<td>");
out.println("<input type='text' name='name'>");
out.println("</td>");
out.println("</tr>");

out.println("<tr>");
out.println("<td>");
out.println("password:");
out.println("</td>");
out.println("<td>");
out.println("<input type='text' name='pwd'>");
out.println("</td>");
out.println("</tr>");

out.println("<tr >");
out.println("<td colspan='2'>");
out.println("<input type='submit' value='提交'>");
out.println("</td>");
out.println("</tr>");

out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
                out.flush();
}
}

package testParameter;

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;

public class GetTableP extends HttpServlet{

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

resp.setContentType("text/html;charset=gb2312");
PrintWriter out=resp.getWriter();

String name=req.getParameter("name");
String pwd=req.getParameter("pwd");

out.println("<html>");
out.println("<body>");
out.println(name);
out.println("<hr>");
out.println(pwd);
out.println("</body>");
out.println("</html>");
                out.flush();

}



}

3:通过setAttribute("name",data)和getAttribute("name");
其中data可以是任何数据类型,另一个servlet通过名字获取该对象getAttribute("name");
注:setAttriute可以被request,session,ServletContext等对象使用。以下以request为例:
Java代码 
package testParameter;  
 
import java.io.IOException;  
 
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
 
public class AttributeA extends  HttpServlet{  
 
    @Override 
    protected void service(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
          
        String data="this is just a test,wo need more test for this";  
        req.setAttribute("name", data);  
        req.getRequestDispatcher("/attributeb").forward(req, resp);  
    }  
 
}  
package testParameter;  
 
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;  
 
public class AttributeB extends HttpServlet{  
 
    @Override 
    protected void service(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
       
         String name=(String)req.getAttribute("name");  
           
         resp.setContentType("text/html;charset=gb2312");  
         PrintWriter out=resp.getWriter();  
           
         out.println("<html>");  
         out.println("<body>");  
         out.println(name);  
         out.println("</body>");  
         out.println("</html>");  
                 out.flush();  
    }  
      
      
 

你可能感兴趣的:(servlet)