jsp/servlet页面出现问题 [英] Problem with jsp/servlet page

查看:75
本文介绍了jsp/servlet页面出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的JSP页面.我有一个EJB,在其中有一个会话bean. 我有一个JSP页面和一个Servlet,但情况很奇怪.

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 situation.

当我单击页面上的执行时,这将变成白页,并且不会给我结果. 我在这里发布了我的代码,请您帮忙.

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>

推荐答案

当您使用老式的 scriptlet (那些<% %>东西)以及其中一种时,JSP将被清空. scriptlet 在响应已经提交时引发了异常.现在显示错误页面为时已晚.浏览器最后显示的是半生不熟的HTML页面(由JSP生成的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.

与实际问题无关,您的方法非常笨拙.您根本不需要 scriptlets .只需使用EL(那些${}东西).它可以立即访问请求参数.例如

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}" />

(有关其他课程,请使用JSTL fn:escapeXml()防止XSS)

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

您甚至不需要将它们复制为servlet中的请求属性.您也不应将结果存储为会话属性(在同一会话中的所有浏览器窗口/选项卡之间共享结果,您不希望将其用于基于请求的变量).将其存储为请求属性

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);

并通过EL按如下方式访问它,它可以通过其名称立即访问页面/请求/会话/应用程序范围的属性:

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}

相关问题:

  • JSP/Servlet中的简单计算器(带有可选的Ajax)
  • 如何避免JSP文件中的Java代码
  • Related questions:

    • Simple calculator in JSP/Servlet (with optional Ajax)
    • How to avoid Java code in JSP files
    • 这篇关于jsp/servlet页面出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆