简体   繁体   中英

Use of request.getParameter in JSP results in NullPointerException

Trying to get a query string parameter and take an appropriate action in a JSP page.

Here is a snippet:

<%@ page import="com.companyx.portal.model.LDAPAttributes" %>
<%@ page import="com.companyx.portal.service.LDAPAttributesLocalServiceUtil" %>
<%@ page import="com.liferay.portal.model.User" %>
<%@ page import="com.liferay.portal.util.PortalUtil" %>
<%@ page import="javax.servlet.http.HttpServletRequest" %>

<%
User user = PortalUtil.getUser(request);
String screenName = user.getScreenName();
LDAPAttributes attr = LDAPAttributesLocalServiceUtil.getLDAPAttributes(screenName);
String store = attr.getLegacyStoreNo();
String org = request.getParameter("org");
%>

...more code here...

<html>
...html code here...
<form name="LoginForm" action="check_login.php">
    <input type="hidden" name="LOGIN_NAME" size="20" value="<%= store %>" />
    <input type="hidden" name="LOGIN_PASSWORD" size="20" value="<%= store %>" />
    <input type="hidden" name="ORGANIZATION" value="<%= org %>" />
</form>

When the following lines are absent:

 String org = request.getParameter("org");
 ...
 <input type="hidden" name="ORGANIZATION" value="<%= org %>" />

The script works just fine, but I need to capture an 'org' parameter from the query string, write it into the generated form and submit it. When those lines are present, though, I get a 500 error.

Any thoughts?

Chances are you're working with the PortletRequest and not the HttpServletRequest .

HttpServletRequest realRequest = PortalUtil.getHttpServletRequest(request);

String organization = realRequest.getParameter("org");
String org = request.getParameter("ORGANIZATION");

try this,

<input id ="org" type="hidden" name="org" value="" />

Then you can add any value to value property.

 String org = request.getParameter("org"); // get from input hidden id

If you are passing org in the query string properly , then your code will work correctly.

And if it is not working properly, use request.setAttribute("org",org_value) instead of passing org value as query parameter and get the org value in jsp using request.getAttribute("org").toString . It will definitely work.

If your problem is not solved even after trying what Andrew Thompson has asked. Incase you are using Tomcat, can you try changing the name of "org" variable to something else. I don't remember exactly but once I have faced a similar problem in one of the versions of Tomcat where "org" variable name was not allowed.

if i use request.getParameter() two times in the same jsp page.

for the 2nd time it shows null.

like

if(request.getParameter("frmdttxt") != request.getParameter("start_date")){
                    out.println("we are here");
                    date_flag="DNM";                        
                }else{

                    date_flag="DM";
                }




if(request.getParameter("page_number")==null){
                //System.out.println("we are here");
                frmdt = request.getParameter("frmdttxt");
                todt = request.getParameter("todttxt");
                stat = request.getParameter("status");
                regn = request.getParameter("region");

                }

then frmdt is null.

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