简体   繁体   English

使用Java Servlet(Jetty)从URL检索数据

[英]Retrieving data from a URL using a Java Servlet (Jetty)

I am trying to attach a parameter to a URL on a servlet: 我正在尝试将参数附加到servlet上的URL:

<a href="another_servlet?myVariable=<%=thevariable>> Go to this </a>

to be retrieved as a variable on another servlet that is called. 被作为另一个servlet上的变量检索。

which contains the following code : 其中包含以下代码:

// Database access with a servlet
package ubiserv.simple;

import java.io.*;
import java.util.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Product extends HttpServlet {
public void service(HttpServletRequest request,HttpServletResponse response)
        throws IOException, ServletException{

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

It seems that I can use this code to fetch what I need 看来我可以使用此代码来获取所需的信息

request.getAttribute

but I have searched almost everywhere and I cannot figure out how to put it all together. 但是我几乎在所有地方都进行了搜索,无法弄清楚如何将它们组合在一起。 Thanks in advance. 提前致谢。

No, those are query parameters. 不,这些是查询参数。 Use this to get that value: 使用它来获得该值:

String myVariable = request.getParameter("myVariable");

Check the javadocs: 检查javadocs:

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM