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();
}
}
第一种方式:通过超链接传递数据
第二种方式:通过表传递取参数。
第三种方式:通过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();
}
}
相关推荐
### JSP与Servlet之间的传值详解 #### 一、引言 在Web开发中,Java Server Pages (JSP) 和 Servlet 是两种非常重要的技术。它们通常被用来构建动态网页和处理用户请求。JSP 主要负责展示层逻辑,而 Servlet 更多...
这种机制允许Servlet之间共享同一个HttpServletRequest对象和HttpServletResponse对象,从而实现数据和控制流的传递。 ##### 2.2 工作原理 - **发起请求**:客户端首先向服务器发送HTTP请求。 - **处理请求**:...
Applet与Servlet之间的通信主要有以下四种方法: 1. **通过HTTP请求**:Applet可以使用URLConnection或HttpClient类向Servlet发送HTTP GET或POST请求。Servlet接收请求后处理数据,并通过HTTP响应将结果返回给...
数据不能直接通过`request`传递,但可以通过session或URL参数传递。 3. **使用JSP内置对象**: Servlet可以将数据放入`request`或`session`,然后在JSP中直接使用EL表达式或脚本let声明来访问这些数据。 例如,...
总结,"超强过滤器"是解决JSP-Servlet之间中文参数GET-POST传递问题的有效手段,通过统一设定请求的字符编码,确保在整个Web应用中中文数据能够正确无误地传输和处理。对于大型项目,这样的全局解决方案可以大大提升...
在本文中,我们将深入探讨Ajax(异步JavaScript和XML)与Java Servlet之间的数据传输机制,这是Web开发中的重要组成部分。Ajax技术允许Web应用程序在不刷新整个页面的情况下与服务器进行交互,提高了用户体验。而...
本文将详细讲解如何通过`request`和`response`对象实现在JSP与Servlet之间的传值。 首先,我们来看JSP传值给Servlet的方式: 1. **Form表单传值**:这是最常见的方法,用户在JSP页面填写表单后提交,表单数据通过...
该项目是一款基于Java与HTML开发的Servlet之间Cookie数据共享的设计源码,共包含25个文件,包括18...主要功能是通过Cookie技术实现两个Servlet之间的数据共享,适用于需要在不同组件间传递用户数据的Web应用开发场景。
它提供了一种在Servlet之间传递信息的方式,以及获取应用级的初始化参数。 7. **请求和响应对象**:HttpServletRequest和HttpServletResponse是处理HTTP请求和响应的主要对象。HttpServletRequest包含了请求的所有...
总之,JSP、JavaScript和Servlet之间的数据传递涉及多种机制,包括请求转发、重定向、会话管理、隐藏字段、AJAX、Cookie、HTTP头以及MVC模式等。理解并熟练运用这些方法,对于提升Web应用程序的交互性和用户体验至关...
在Servlet中,可以通过多种方式在Servlet之间或者Servlet与JSP页面之间传递数据,例如: - **setAttribute()**:设置属性。 - **getAttribute()**:获取属性。 - **RequestDispatcher**:使用RequestDispatcher接口...
Javabean提供了getter和setter方法,使得数据的访问和修改更加方便,同时也便于在Servlet和JSP之间传递数据。 JSP(JavaServer Pages)是另一种用于动态生成Web内容的技术。在留言板系统中,JSP通常用于渲染HTML...
在本项目中,JSON用于在Servlet和JSP之间传递数据。 - AJAX(Asynchronous JavaScript and XML):一种在不刷新整个页面的情况下与服务器交换数据并更新部分网页的技术,使得数据的获取和页面的更新更为平滑、快速。...
ServletContext是整个Web应用程序的共享上下文,它可以用来存储全局信息,或者在不同Servlet之间共享数据。 `ServletInfo`方法返回Servlet的相关信息,如开发者、版本等,主要用于文档和调试目的。 总的来说,...
在IT领域,特别是Web开发中,Servlet与JSP之间的数据传递是实现动态网页的重要机制之一。根据提供的文件信息,我们可以深入解析servlet如何将对象传给JSP的关键知识点。 ### Servlet与JSP简介 Servlet是一种Java...
同时,前端界面使用EasyUI框架构建,通过JSON格式在Servlet和前端之间传递数据。Properties文件则用于存储数据库连接的配置信息。这个实例为学习和理解这些技术如何协同工作提供了一个很好的实践平台。
每当一个客户请求一个HttpServlet 对象,该对象的service() 方法就要被调用,而且传递给这个方法一个“请求”(ServletRequest)对象和一个“响应”(ServletResponse)对象作为参数。 在 HttpServlet 中已存在 ...
完成数据库操作后,Servlet可以将数据传递给JSP页面进行渲染展示。 5. JSP 页面与Hibernate 结合: JSP页面可以通过EL表达式直接访问由Servlet传递过来的对象属性,或者使用JSTL的c:forEach标签遍历集合数据。此外...
`ServletContext`接口代表了整个Web应用的共享环境,它可以在所有Servlet之间传递信息。可以使用`getInitParameter()`获取全局初始化参数,`setAttribute()`和`getAttribute()`用于存储和检索对象。 六、Servlet的...