本文介绍了Servlet在processRequest方法中给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


关于我的servlet异常,我有一个小问题:

javax.ejb.EJBException

输出屏幕显示错误是在processRequest方法中的servlet中,该方法是:

公共类ViewServlet扩展了HttpServlet {

/**
*处理对HTTP< code> GET</code>和< code> POST</code>方法.
* @param请求servlet请求
* @param响应servlet响应
* @@如果发生特定于servlet的错误,则抛出ServletException
* @如果发生I/O错误,则抛出IOException
*/
受保护的void processRequest(HttpServletRequest请求,HttpServletResponse响应)
引发ServletException,IOException {
response.setContentType("text/html; charset = UTF-8");
PrintWriter out = response.getWriter();
字符串s = request.getParameter("Button");
字符串w = request.getParameter("input");
HttpSession session = request.getSession(false);
ListRemote r =(ListRemote)session.getAttribute("R");
如果(r == null)
{
session = request.getSession();

试试{
上下文ctx = new InitialContext();
r =(ListRemote)ctx.lookup(ListRemote.class.getName());
session.setAttribute("R",r);
}
catch(NamingException ne){}}
如果(s.equalsIgnoreCase(添加"))
{
r.AddMethod(w);
response.sendRedirect("/index.jsp");
}
其他
{
ArrayList< String> WordList = r.ShowMethod();
对于(String i:WordList)
{
out.println(i);


}

}
尝试{
/* TODO在这里输出您的页面
out.println(& lt; html>");
out.println(& lt; head>");
out.println(& lt; title> Servlet WordServlet& lt//title>");
out.println(& lt;/head>");
out.println(& lt; body>");
out.println(< h1> Servlet WordServlet位于" + request.getContextPath()+</h1>"));
out.println(& lt;/body>");
out.println(& lt;/html>");
*/
}


终于{
out.close();
}
}

//< editor-fold defaultstate ="collapsed" desc ="HttpServlet方法.单击左侧的+号以编辑代码.">
/**
*处理HTTP< code> GET</code>方法.
* @param请求servlet请求
* @param响应servlet响应
* @@如果发生特定于servlet的错误,则抛出ServletException
* @如果发生I/O错误,则抛出IOException
*/
@Override
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)
引发ServletException,IOException {
processRequest(请求,响应);
}

/**
*处理HTTP< code> POST</code>方法.
* @param请求servlet请求
* @param响应servlet响应
* @@如果发生特定于servlet的错误,则抛出ServletException
* @如果发生I/O错误,则抛出IOException
*/
@Override
受保护的void doPost(HttpServletRequest请求,HttpServletResponse响应)
引发ServletException,IOException {
processRequest(请求,响应);
}

/**
*返回servlet的简短描述.
* @返回包含servlet描述的字符串
*/
@Override
public String getServletInfo(){
返回简短说明";
}//</editor-fold>

}

另外,如果其中也有任何代码错误,我也会发布我的jsp页面:

<%@ page contentType ="text/html" pageEncoding ="UTF-8"%>
<!DOCTYPE html>
& lt; html>
& lt; head>
& lt; meta http-equiv ="Content-Type" content ="text/html; charset = UTF-8">
& lt; title> JSP页面& lt;/title>
& lt//head>
& lt; body>
< h1>实验室3!</h1>
& lt; form method ="POST" action ="ViewServlet">
& lt; input type ="text" name ="txtinput">& lt//input>
& lt; input type ="submit" name ="Button" value =添加">& lt;/input>
& lt; input type ="submit" name ="Button" value =列表">& lt;/input>
& lt;/form>
& lt;/body>
& lt;/html>

Hi,
I have a small question regarding the exception i get for my servlet:

javax.ejb.EJBException

The output screen shows that the error is in my servlet in the processRequest method which is:

public class ViewServlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String s=request.getParameter("Button");
String w=request.getParameter("input");
HttpSession session=request.getSession(false);
ListRemote r=(ListRemote) session.getAttribute("R");
if (r==null)
{
session=request.getSession();

try{
Context ctx=new InitialContext();
r=(ListRemote) ctx.lookup(ListRemote.class.getName());
session.setAttribute("R", r);
}
catch(NamingException ne){}}
if (s.equalsIgnoreCase("Add"))
{
r.AddMethod(w);
response.sendRedirect("/index.jsp");
}
else
{
ArrayList<String> WordList=r.ShowMethod();
for (String i:WordList)
{
out.println(i);


}

}
try {
/* TODO output your page here
out.println("&lt;html>");
out.println("&lt;head>");
out.println("&lt;title>Servlet WordServlet&lt;/title>");
out.println("&lt;/head>");
out.println("&lt;body>");
out.println("<h1>Servlet WordServlet at " + request.getContextPath () + "</h1>");
out.println("&lt;/body>");
out.println("&lt;/html>");
*/
}


finally {
out.close();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}

Also, I am posting my jsp page if there is any code mistake in that too:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
&lt;html>
&lt;head>
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
&lt;title>JSP Page&lt;/title>
&lt;/head>
&lt;body>
<h1>Lab 3!</h1>
&lt;form method="POST" action="ViewServlet">
&lt;input type="text" name="txtinput">&lt;/input>
&lt;input type="submit" name="Button" value="Add">&lt;/input>
&lt;input type="submit" name="Button" value="List">&lt;/input>
&lt;/form>
&lt;/body>
&lt;/html>

推荐答案


这篇关于Servlet在processRequest方法中给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:41