I am working on an assignment that I have. It is fairly straight forward. An HTML form which contains a single input is submitted to a Servlet that grabs the parameter, creates a message based on the parameter, adds the message as an attribute to the request, and uses a requestdispatcher to forward to a jsp to display the message.
我正在完成一项任务。这是相当直接的。将包含单个输入的HTML表单提交给Servlet,该Servlet抓取参数,基于参数创建消息,将消息作为属性添加到请求,并使用requestdispatcher转发到jsp以显示消息。
I have a requirement that if the parameter is missing, I need to display an error page. The catch is that I can't explicitly check for a null, or use a try/catch block. My guess is that the goal is to define an error page in the web.xml page to handle errors of a certain type, but the problem is, if I cant check to see if the request parameter is null, or use a try/catch, how do I know if I need to throw an exception? Any ideas?
我要求如果参数丢失,我需要显示错误页面。问题是我无法显式检查null,或使用try / catch块。我的猜测是目标是在web.xml页面中定义一个错误页面来处理某种类型的错误,但问题是,如果我无法检查请求参数是否为null,或者使用try / catch ,我怎么知道是否需要抛出异常?有任何想法吗?
4 个解决方案
#1
0
If you intend to create a message based on the parameter then it is a bit difficult to see how you can achieve this if you cannot check the parameters value (for null for instance). Presumably you call...
如果您打算基于参数创建消息,那么如果您无法检查参数值(例如,为null),则很难看到如何实现此目的。大概是你打电话......
HttpServletRequest.getParameter()
Which returns the parameter value or null if the parameter is missing.
返回参数值,如果缺少参数,则返回null。
#2
0
In web.xml
, you can mention exceptions also.
在web.xml中,您也可以提及例外。
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
Or you can take help from this link to create new servlets to handle errors. Servlet Exception Handling
或者,您可以从此链接获取帮助以创建新的servlet来处理错误。 Servlet异常处理
#3
0
Normally to check for null you'd do:
通常要检查null,你会这样做:
String param = request.getParameter("param");
if(param!=null)
If they don't want you doing that, probably they want you to use the DOT operator to cause the NullPointerExpection
如果他们不希望你这样做,他们可能希望你使用DOT操作符来导致NullPointerExpection
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, NullPointerException
{
String param = request.getParameter("param");
if(param.equals("x"))
{
//if param was null, simply using
//the DOT operator on param would throw
// the NullPointerExpection
response.sendRedirect("x.jsp");
return;
}
}
To avoid having to explicitly check for null and avoid the NullPointerExpection you could do:
为了避免显式检查null并避免NullPointerExpection,您可以这样做:
if("x".equals(param))
#4
0
In the web.xml, you can specify an error page, like this.
Let's assume you want to catch HTTP400, 500 and exceptions:
在web.xml中,您可以指定错误页面,如下所示。假设您要捕获HTTP400,500和异常:
<error-page>
<error-code>400</error-code>
<location>/errorpage.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/errorpage.html</location>
</error-page>
(as suggested by Arjit)
(由Arjit建议)
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorpage.html</location>
</error-page>
And then to put it all together, as suggested by DeveloperWJK, in the servlet:
然后按照DeveloperWJK的建议将它们放在servlet中:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, NullPointerException
{
String param = request.getParameter("param");
if(param.equals("x"))
{
response.sendRedirect("x.jsp");
return;
}
}
#1
0
If you intend to create a message based on the parameter then it is a bit difficult to see how you can achieve this if you cannot check the parameters value (for null for instance). Presumably you call...
如果您打算基于参数创建消息,那么如果您无法检查参数值(例如,为null),则很难看到如何实现此目的。大概是你打电话......
HttpServletRequest.getParameter()
Which returns the parameter value or null if the parameter is missing.
返回参数值,如果缺少参数,则返回null。
#2
0
In web.xml
, you can mention exceptions also.
在web.xml中,您也可以提及例外。
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
Or you can take help from this link to create new servlets to handle errors. Servlet Exception Handling
或者,您可以从此链接获取帮助以创建新的servlet来处理错误。 Servlet异常处理
#3
0
Normally to check for null you'd do:
通常要检查null,你会这样做:
String param = request.getParameter("param");
if(param!=null)
If they don't want you doing that, probably they want you to use the DOT operator to cause the NullPointerExpection
如果他们不希望你这样做,他们可能希望你使用DOT操作符来导致NullPointerExpection
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, NullPointerException
{
String param = request.getParameter("param");
if(param.equals("x"))
{
//if param was null, simply using
//the DOT operator on param would throw
// the NullPointerExpection
response.sendRedirect("x.jsp");
return;
}
}
To avoid having to explicitly check for null and avoid the NullPointerExpection you could do:
为了避免显式检查null并避免NullPointerExpection,您可以这样做:
if("x".equals(param))
#4
0
In the web.xml, you can specify an error page, like this.
Let's assume you want to catch HTTP400, 500 and exceptions:
在web.xml中,您可以指定错误页面,如下所示。假设您要捕获HTTP400,500和异常:
<error-page>
<error-code>400</error-code>
<location>/errorpage.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/errorpage.html</location>
</error-page>
(as suggested by Arjit)
(由Arjit建议)
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/errorpage.html</location>
</error-page>
And then to put it all together, as suggested by DeveloperWJK, in the servlet:
然后按照DeveloperWJK的建议将它们放在servlet中:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException, NullPointerException
{
String param = request.getParameter("param");
if(param.equals("x"))
{
response.sendRedirect("x.jsp");
return;
}
}