日期:2014-05-17  浏览次数:20673 次

一个不足30行代码jsp页面问题,80分求解答
HTML code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String strName=null;
String strThing=null;
try{
strName=request.getParameter("name");
strThing=request.getParameter("thing");
if((strName==null)||(strName.length()==0)){throw new Exception("null strName");}
if((strThing==null)||(strThing.length()==0)){throw new Exception("null strThing");}
session.setAttribute("name",strName);
session.setAttribute("thing",strThing);
response.sendRedirect("display.jsp");
}
catch(Exception e){}
 %>

<html>
  <body>
    <form action="" method="get">
    <table>
    <tr><td>名称:</td><td><input name="name" type="text" /></td></tr>
    <tr><td>事件:</td><td><input name="thing" type="text" /></td></tr>
    <tr><td align="right"></td> 
    <td><button type="submit">提交</button><button type="reset">重置</button></td></tr>
    </table>
    </form>
  </body>
</html>

大家先说说这个页面能正常打开不?(我基础不是很好,才学jsp几个月)

------解决方案--------------------
你form里用得get 就是把当前页面的数据 用get方式提交 url里不就有参数了?然后你说你获取url里的参数 会得不到吗?
你用了response.sendRedirect("display.jsp");
重定向 怎么会不到 display.jsp呢?
------解决方案--------------------
catch(Exception e){}就因为这一句你捕获了异常,没有做任何处理,加上out.print(e);也只是将异常信息输出,保证了下面代码的正常执行,你要是想要不能执行的效果很简单:
strName=request.getParameter("name");
strThing=request.getParameter("thing");
if((strName==null)||(strName.length()==0)){throw new Exception("null strName");}
if((strThing==null)||(strThing.length()==0)){throw new Exception("null strThing");}
session.setAttribute("name",strName);
session.setAttribute("thing",strThing);
response.sendRedirect("display.jsp");
只要去掉try,catch就可以了