java 将隐藏的输入值从 jsp 传递到 servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29092537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:41:04  来源:igfitidea点击:

Passing hidden input value from jsp to servlet

javajspservlets

提问by Amber

I'm trying to generate an excel file when a link is clicked without being redirected anywhere. I make it into the servlet but when I try to get the value of a hidden input, I get a null pointer exception. The code I'm using is

我试图在单击链接时生成一个 excel 文件而没有被重定向到任何地方。我将它放入 servlet,但是当我尝试获取隐藏输入的值时,我得到一个空指针异常。我使用的代码是

JSP

JSP

<html:form action="/UploadExcel.do" enctype="multipart/form-data" method="post">
<a href="#" onclick="ExcelFiller.fill">Download Excel</a>
<input type="hidden" name="refBgcId" id="refBgcId" value="84"></input>
</html:form>

Servlet

小服务程序

public class ExcelFiller extends HttpServlet {
private static final long serialVersionUID = 1L;

public void init(ServletConfig config) throws ServletException {    
    super.init(config);
}

public void destroy() {

}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("Made it to the servlet");
    doPost(request,response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("In doPost");
    HttpSession session=(HttpSession) request.getSession(false);

    String loggedInUserId = (String)session.getAttribute("strUserId");
    try{
        String referenceid = request.getParameter("refBgcId").toString();/*NULL POINTER EXCEPTION*/
        ArrayList<String> details = new ArrayList<String>(); 
        details = UploadBGCDAO.getDetails(referenceid);
        createExcel(request,response,details, loggedInUserId);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

I have tried finding the solution in the answers to similar questions on this site but I still can't make this work. Can someone please tell me what I'm doing wrong?

我已经尝试在本网站上类似问题的答案中找到解决方案,但我仍然无法完成这项工作。有人可以告诉我我做错了什么吗?

回答by Brijesh Bhatt

Input parameter will be passed to the servlet only when you submit a form, not when your anchor tag is clicked.

输入参数只会在您提交表单时传递给 servlet,而不是在单击锚标记时传递给 servlet。

If you want to pass a parameter append the parameter in a query String.

如果要传递参数,请将参数附加到查询字符串中。

回答by saikumarm

<form action="ExcelFiller.fill">
<input type="hidden" name="refBgcId" id="refBgcId" value="84"></input>
<input type="submit" value="Download Excel"></input>
</form>

and in the servlet make the following changes. the name of the attribute is refBgcId. change the statement

并在 servlet 中进行以下更改。属性的名称是refBgcId. 更改声明

String referenceid = request.getParameter("ref_id").toString();

String referenceid = request.getParameter("ref_id").toString();

to

String referenceid = request.getParameter("refBgcId").toString();

String referenceid = request.getParameter("refBgcId").toString();

回答by Prashant

you can pass parameters in URL in Java Script as :

您可以在 Java Script 中将 URL 中的参数传递为:

var refBgcId=84;
var url = "/UploadExcel.do?refBgcId="+refBgcId;
window.location.href =url;

and in servlet you can access as

在 servlet 中,您可以访问为

   String refBgcId= request.getParameter("refBgcId").toString();

回答by Jayant

String referenceid = request.getParameter("refBgcId").toString(); // here refBgcId is a name...dont confuse with name and Id pass name from jsp

String referenceid = request.getParameter("refBgcId").toString(); // 这里 refBgcId 是一个名称...不要与来自 jsp 的名称和 Id 传递名称混淆

and then in servlet try this

然后在servlet中试试这个

String referenceid = request.getParameter("refBgcId").toString();

String referenceid = request.getParameter("refBgcId").toString();