jsp/servlet页面的问题
我想创建一个简单的 JSP 页面。我有一个EJB,其中有一个会话bean。 我有一个 JSP 页面和一个 Servlet,但我遇到了一个奇怪的情况。
当我在页面上单击“执行”时,这会变成一个白页并且不会给我结果。 我在这里发布我的代码,你能帮我吗?
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> -</b><input type='radio' name="oper" value='-' /></li>
<li><b>*</b><input type='radio' name="oper" value='*' /></li>
<li> <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>
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> -</b><input type='radio' name="oper" value='-' /></li>
<li><b>*</b><input type='radio' name="oper" value='*' /></li>
<li> <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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用老式 scriptlet(那些
<% %>
东西)并且其中一个 scriptlet 抛出异常时,JSP 将会空白响应已提交时发生异常。这时显示错误页面已经太晚了。浏览器最终会得到一个半生不熟的 HTML 页面(JSP 生成的 HTML 不完整,浏览器通常会变成空白)。您应该阅读服务器日志中的异常并相应地修复代码。与实际问题无关,您的方法非常笨拙。您根本不需要scriptlet。只需使用 EL(那些
${}
的东西)。它可以即时访问请求参数。例如(额外的课程要点:使用 JSTL
fn:escapeXml()
来防止 XSS)您甚至不需要将它们复制为 servlet 中的请求属性。您也不应该将结果存储为会话属性(它将在同一会话中的所有浏览器窗口/选项卡之间共享,您不希望将其用于基于请求的变量)。将其存储为请求属性
并通过 EL 访问它,如下所示,它可以通过其名称即时访问 page/request/session/application 范围的属性:
相关问题:
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.(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
and access it by EL as follows, it has instant access to page/request/session/application scoped attributes by just its name:
Related questions: