简体   繁体   中英

Passing hidden input value from jsp to servlet

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

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?

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

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

<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

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

to

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

you can pass parameters in URL in Java Script as :

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

and in servlet you can access as

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

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

and then in servlet try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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