java - 在 JSP 中使用 request.getParameter 会导致 NullPointerException

标签 java jsp nullpointerexception liferay

尝试获取查询字符串参数并在 JSP 页面中采取适当的操作。

这是一个片段:

<%@ 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>

当缺少以下行时:

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

该脚本工作得很好,但我需要从查询字符串中捕获“org”参数,将其写入生成的表单中并提交。但是,当这些行出现时,我会收到 500 错误。

有什么想法吗?

最佳答案

您可能正在使用 PortletRequest 而不是 HttpServletRequest

HttpServletRequest realRequest = PortalUtil.getHttpServletRequest(request);

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

关于java - 在 JSP 中使用 request.getParameter 会导致 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8936942/

相关文章:

java session bean - 重新加载数据?

scala - 数据框过滤器给出 NullPointerException

java - JTextArea 不调整大小

java - 将列表重新排序为一个接一个

java - char[] 转换为 ascii 和十进制值( double )java

spring - 在IDE中运行Spring Boot时找不到JSP,但手动运行时可以工作

java - 家庭作业问题。为日历约会程序编写方法时遇到问题

javascript - Netbeans 字段验证(字母\数字\长度)

java - 空指针异常 : android editText

android - 将父 fragment 转换为主 fragment 时出现空指针异常