测试目的:
当页面a.jsp向页面b.jsp提交
b.jsp通过request.getParameter("属性名")获得相应提交值
当a.jsp没有提交相应属性值时,通过Exception_other类来处理该异常
问题:报异常
----------------- a.jsp ---------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <head></head>
  <body>
   <form name="form1" method="post" action="b.jsp">
   请输入用户姓名:<input type="text" name="username">
    <input type="submit" name="Submit" value="提交">
   </form>
  </body>
</html>---------------- b.jsp -----------------
<%@ page language="java" import="java.util.*,com.cn.Exception_other" pageEncoding="UTF-8"%>
<html>  
  <body>
    <% 
    String username = 
     Exception_other.operatGetMothod(request,"username");
    %>
    <%=username%>
  </body>
</html>
----------- com.cn.Exception_other ----------
package com.cn;import javax.servlet.http.HttpServletRequest;public class Exception_other {

private static String getStr = "";
public static String operatGetMothod(HttpServletRequest request,String str){

try {
getStr = request.getParameter(str);
if(getStr.equals(str)){
getStr = str;
}else{
getStr = "";
}
} catch (Exception e) {
getStr = "";
}
return getStr;
}
}
========================所报错误========================
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception javax.servlet.ServletException: javax/servlet/http/HttpServletRequest
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.b_jsp._jspService(b_jsp.java:63)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
com.cn.Exception_other.operatGetMothod(Exception_other.java:11)
org.apache.jsp.b_jsp._jspService(b_jsp.java:49)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
--------------------------------------------------------------------------------

解决方案 »

  1.   

    <%@ page language="java" import="java.util.*,com.cn.Exception_other" pageEncoding="UTF-8"%>
    这里面是不是要加上
    javax.servlet.http.HttpServletRequest
      

  2.   

    getStr = request.getParameter(str);
    if(getStr.equals(str)){//得判断getStr是否为null
    getStr = str;
    }else{
    getStr = "";
    }
      

  3.   

    >supermanyan10() '引包javax.servlet.http.HttpServletRequest,'
                     不是jsp里面就有该类的对象request了么,不用再引包了吧。
    >
    iisbocai(波菜)   '//得判断getStr是否为null'
                     不在if条件里判断该情况,把它放到else情况也可以的
                     (为null时的情况不涉及单独的业务处理)
      

  4.   

    java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
    因为它报的是这个错
    所以我觉得是它没找到这个包
      

  5.   

    getStr.equals(str)这里如果getStr为null就会引发异常。
    但会被try catch块捕获。
    不过,不管你是基于什么样的考虑,还是对getStr进行null判断较好。
    楼上的兄弟说的有道理。从报出的错误来看应该是没有找到HttpServletRequest类。
    先看看必要的包有没有导进来。