Use of request.getParameter in JSP results in NullPointerException

对着背影说爱祢 提交于 2019-12-14 03:26:19

问题


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?


回答1:


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

HttpServletRequest realRequest = PortalUtil.getHttpServletRequest(request);

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



回答2:


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



回答3:


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



回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/8936942/use-of-request-getparameter-in-jsp-results-in-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!