简体   繁体   English

Servlet异常处理:使用不同的消息转发到同一错误页面

[英]Servlet exception handling: forwarding to the same error page with different messages

How can I handle exception in a servlet forwarding to the same error page with different messages? 如何在Servlet中使用不同的消息转发到同一错误页面时处理异常? In my servlet I have the following doGet (simplified here) method, with a switch case: 在我的servlet中,我具有以下doGet(在此简化)方法,并带有切换用例:

public class NewServlet extends HttpServlet {
//[...]
protected void doGet(
    //[...]
        try {
            DAO ldapdao = new DAO();
            List<Entry> entries = null;

            switch (enumPage.fromString(operation)) {                    
//  [...]
                case add:
                    String values[] = request.getParameterValues("item");
                    try {
                        ldapdao.addentry(values);
                        link = "entryadded.jsp";
                    } catch (Exception exp) {                                   // 2                            
                        throw new MyException("Entry Not Added");
                    }
                    break;


                case remove:
                    entries = ldapdao.searchEntry(request.getParameter("item"));
                    if (entries.isEmpty()) {
                        throw new MyException("Entry Not Removed");                              //3
                    } else {
                        ldapdao.remove(request.getParameter("item"));
                        link = "entryremoved.jsp";
                    }
                    break;

                case modified:
                    String values1[] = request.getParameterValues("item");
                    try {
                        ldapdao.modify(values1);
                        link = ("modified.jsp");
                    } catch (Exception exp) {                                   //4
                        throw new MyException("Entry Not Modified");
                    }
//  [...]

Then I have my custom exception: 然后有我的自定义异常:

package com.mycompany.test_servlet;
import javax.servlet.ServletException;
public class MyException extends ServletException {
private String message = null;
public MyException() {
    super();
}
public MyException(String message) {
    super(message);
}
public MyException(Throwable cause) {
    super(cause);        
}    
}

and the web.xml file: 和web.xml文件:

<error-page>
    <location>/Error.jsp</location>
</error-page>

Try indicating the exception type: 尝试指出异常类型:

<error-page>
    <exception-type>com.mycompany.test_servlet.MyException</exception-type>
    <location>/Error.jsp</location>
</error-page>

You can try to set attribute in request object, while catching the exception 您可以尝试在请求对象中设置属性,同时捕获异常

request.setAttribute("exception", ex.getMessage());

and then get this attribute in error.jsp, for example: 然后在error.jsp中获取此属性,例如:

                        <% if (request.getAttribute("exception") != null) {  %>
                        <tr>
                            <td colspan='2'><p align=right>Authentication Error (try again):<br>
                                <%=request.getAttribute("exception")%>
                            </p></td>
                        </tr>
                        <%}%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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