Servlet之间传递程数据的方式

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

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

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

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")获得参数的值:

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 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为例:
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();
}



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值