开发者

Problem with jsp/servlet page

I want to create a simple JSP page. I have an EJB, in this there is a session bean. I have a JSP page and a Servlet, but I have a strange situatio开发者_运维百科n.

When I click execute on my page, this turns in a white page and don't give me the result. I post here my code, can you help me please.

Servlet:

package web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.*;
import javax.servlet.http.*;

import ejb.calc;
/**
 * Servlet implementation class calcServlet
 */
public class calcServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public calcServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  HttpSession session=request.getSession(true); 
  RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/index.jsp"); 

  float a=Float.parseFloat(request.getParameter("n1"));
  float b=Float.parseFloat(request.getParameter("n2"));
  char oper=request.getParameter("oper").charAt(0);
  float result=0;

  try {
   Context ctx=new InitialContext();
  // call the calcImpl class of the SimpleCalculator EJB with the mappedName
   calc cl=(calc) ctx.lookup("Firstcalc");
   switch(oper){

   case '+': result=cl.sum(a, b); break;

   case '-': result =cl.minus(a, b); break;

   case '*': result =cl.mult(a, b); break;

   case '/': result =cl.div(a, b); break;
  }
   session.setAttribute("result",result);
   request.setAttribute("a", a);

   request.setAttribute("b", b);
  }
  catch(NamingException e)
  {session.setAttribute("erreur: ",e.getMessage());
  }rd.forward(request,response);
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }

}

JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <h2> <b> Hello World To The Simple Calculator </b> </h2> <% float a=2,b=1; if (request.getAttribute("a")!=null) a=Float.parseFloat(request.getAttribute("a").toString()); if( request.getAttribute("b")!=null) b=Float.parseFloat(request.getAttribute("b").toString()); %> <form method="post" action="calcServlet"> <b>Number 1:</b><input type='text' name='n1' value="<%=a%>" /> <br/>
    <b>Number 2:</b><input type='text' name='n2' value="<%=b%>" /> <br/>
    <u><b>Options:</b></u> <br/>
    <ul>
    <li><b>+</b><input type='radio' name="oper" value='+' checked /></li>
    <li><b>&nbsp;-</b><input type='radio' name="oper" value='-' /></li>
    <li><b>*</b><input type='radio' name="oper" value='*' /></li>
    <li>&nbsp; <b>/</b><input type='radio' name="oper" value='/' /></li> </ul>
    <b>-------------------------------------------</b> <br/>
    <input type="submit" value="Executer" /> </form>
    <font color='blue'><b>Result is: </b> <%=session.getAttribute("result")%> </font> <br/> <font color='red' >Error: <%=session.getAttribute("error")%></font>
    </body>
    </html>


A JSP will blank out when you use old fashioned scriptlets (those <% %> things) and one of such a scriptlet has thrown an exception while the response has already been committed. It's too late then to display the error page. The browser ends up with a halfbaked HTML page (the HTML generated by JSP is incomplete and the browser will usually go blank). You should read the server logs for the exception and fix the code accordingly.


Unrelated to the actual problem, your approach is pretty clumsy. You don't need scriptlets at all. Just use EL (those ${} things). It has instant access to request parameters. E.g.

<input type="text" name="n1" value="${param.n1}" />

(for extra course points: use JSTL fn:escapeXml() to prevent XSS)

You don't even need to duplicate them as request attributes in the servlet. You should also not store the result as session attribute (it would be shared among all browser windows/tabs in the same session, you don't want to have this for a request based variable). Store it as request attribute

request.setAttribute("result", result);

and access it by EL as follows, it has instant access to page/request/session/application scoped attributes by just its name:

<b>Result is: </b> ${result}

Related questions:

  • Simple calculator in JSP/Servlet (with optional Ajax)
  • How to avoid Java code in JSP files
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜