本帖最后由 Myxiao7 于 2009-09-14 19:47:54 编辑

解决方案 »

  1.   

     onclick="fun_submit()"这个东西干了什么?
      

  2.   

    action="/SearchBook/show.do?cmd=keyword"把里面得cmd用input type="hidden" 替换一下。
      

  3.   

    你将str_search_ky打印出来看看。
      

  4.   

    java2000_net,你好,fun_submit()里面的代码不影响action的值的,可以忽略的,忘记删除了.
    还有 "把里面得cmd用input type="hidden" 替换一下。"这样有什么好处吗,请老紫竹指教,能得到那么多徽章的高手指点,是我的荣幸.
      

  5.   

    qiushyfm,你好,谢谢你的答复,我按照你的做了
    结果:HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
    com.xiao7.book.BookServlet.doGet(BookServlet.java:101)
    com.xiao7.book.BookServlet.doPost(BookServlet.java:114)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.
      

  6.   

    onclick="fun_submit()"是你自己定义的方法吗,
    我把你的代码按原样运行了下,jsp界面什么都没有,
    然后我该了一点,直接用submit提交,没有出现你说的
    情况,你最还是把全部代码发出来,好让大家能跟明白些!
      

  7.   

    其实我是做了些修改的,为了让看官不要那么累的,既然楼上有要求,我就贴出来,请指点:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>页面选择</title>
    <script type="text/javascript"> function fun_submit(){
    if(form_choose.pick[0].checked){
    form_choose.action="/SearchBook/show.do?cmd=checkall";
    }
    else{
    if(form_choose.pick[1].checked){
    form_choose.action="/SearchBook/show.do?cmd=precise";
    }
    else{
    form_choose.action="/SearchBook/show.do?cmd=keyword";
    }
    }
    } function fun_check(){
    if(form_choose.pick[0].checked){
    document.getElementById("tb_pre").style.display="none";
    document.getElementById("tb_key").style.display="none";
    }
    }

    function fun_pre(){
    if(form_choose.pick[1].checked){
    document.getElementById("tb_pre").style.display="";
    document.getElementById("tb_key").style.display="none";
    }

    }

    function fun_key(){
    if(form_choose.pick[2].checked){
    document.getElementById("tb_pre").style.display="none";
    document.getElementById("tb_key").style.display="";
    } }
    </script>
    </head>
    <body>
    <form name="form_choose" method="post" action="" onclick="fun_submit()">
      <p>
        <input type="radio" name="pick"  value="radio" onclick="fun_check()" checked="checked"/>
        查看所有图书<br />
        <input type="radio" name="pick"  value="radio2" onclick="fun_pre()"/>
        精确搜索<br />
      <table id="tb_pre" border="1" style="display:none" >
        <tr>
          <td>ID号码:</td>
          <td><input type="text" name="bookid" /></td>
        </tr>
        <tr>
          <td>书名:</td>
          <td><input type="text" name="bookname" /></td>
        </tr>
        <tr>
          <td>作者:</td>
          <td><input type="text" name="author"/></td>
        </tr>
      </table>
      <input type="radio" name="pick"  value="radio3" onclick="fun_key()"/>
      关键字搜索
      <table id="tb_key" border="1" style="display:none">
        <tr>
          <td>请输入关键字:</td>
          <td><input type="text" name="search_ky" /></td>
        </tr>
      </table>
      <p>
        <input type="reset" name="button"  value="重新选择" />
        <input type="submit" name="button2"  value="搜索" />
      </p>
    </form>
    </body>
    </html>
      

  8.   

    String str_search_ky = (String)request.getAttribute("search_ky");
    试试。
    你能不能加个断点,看看request这个对象里都保存了些什么属性。
    或者用getAttributeNames之类的方法,打印出来看看。
      

  9.   

    我看了半天。
      <table id="tb_key" border="1" style="display:none">
        <tr>
          <td>请输入关键字:</td>
          <td><input type="text" name="search_ky" /></td>
        </tr>
      </table>
    你把这个table隐藏了,不让人填写,怎么会有值呢?
      

  10.   

    服。
    你把style="display:none"去掉,然后选关键字查找,然后填个东西在里面。
    我这里运行的好好的,debug出来也有值。一点问题没有。
      

  11.   

    bayougeng(梦里楚香),你好,我按照你的做了,但是还是抛出空指针异常,我想应该不是style="display:none"这个的问题的,因为当我点击这个单选框的时候
    ----> <input type="radio" name="pick"  value="radio3" onclick="fun_key()"/> 
    --->  function fun_key(){
            if(form_choose.pick[2].checked){
                document.getElementById("tb_pre").style.display="none";
                document.getElementById("tb_key").style.display="";
            }
    这样 table id="tb_key" 会出现的.
    真正的原因是不知道为什么抛出空指针异常.这位朋友,要不我给我的源码给你看看如何. 
      

  12.   

    即使是按照你原有的代码,也没有问题。我把我所有的代码都贴出来。
    index.html<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
    <title>页面选择</title> 
    <script type="text/javascript">  
    function fun_submit(){ 
    if(form_choose.pick[0].checked){ 
    form_choose.action="/ServletTest/hello.do?cmd=checkall"; 
    } else{ 
    if(form_choose.pick[1].checked){ 
    form_choose.action="/ServletTest/hello.do?cmd=precise"; 
    } else{ 
    form_choose.action="/ServletTest/hello.do?cmd=keyword"; 


    } function fun_check(){ 
    if(form_choose.pick[0].checked){ 
    document.getElementById("tb_pre").style.display="none"; 
    document.getElementById("tb_key").style.display="none"; 

    } function fun_pre(){ 
    if(form_choose.pick[1].checked){ 
    document.getElementById("tb_pre").style.display=""; 
    document.getElementById("tb_key").style.display="none"; 

    } function fun_key(){ 
    if(form_choose.pick[2].checked){ 
    document.getElementById("tb_pre").style.display="none"; 
    document.getElementById("tb_key").style.display=""; 


    </script> 
    </head> <body> 
    <form name="form_choose" method="post" action="hello.do" onclick="fun_submit()">  
    <p> <input type="radio" name="pick" value="radio" onclick="fun_check()" checked="checked"/>  查看所有图书<br />  
    <input type="radio" name="pick" value="radio2" onclick="fun_pre()"/>  精确搜索<br />  
    <table id="tb_pre" border="1" style="display:none" >  
    <tr>  
    <td>ID号码:</td>  
    <td><input type="text" name="bookid" /></td>  
    </tr>  
    <tr>  
    <td>书名:</td>  
    <td><input type="text" name="bookname" /></td>  
    </tr>  
    <tr>  
    <td>作者:</td>  
    <td><input type="text" name="author"/></td>  
    </tr>  
    </table>  
    <input type="radio" name="pick" value="radio3" onclick="fun_key()"/>  关键字搜索 
    <table id="tb_key" border="1" style="display:none">
    <!--table id="tb_key" border="1"-->  
    <tr>  
    <td>请输入关键字:</td>  
    <td><input type="text" name="search_ky" /></td>  
    </tr>  
    </table>  
    <p>  <input type="reset" name="button" value="重新选择" />  
    <input type="submit" name="button2" value="搜索" />  
    </p> 
    </form> 
    </body> 
    </html>
    web.xml<?xml version="1.0" encoding="GB2312"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

      <display-name>First Servlet</display-name>
      <description>My First Servlet</description>
     
      <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>young.servlet.MyServlet</servlet-class>
      </servlet>  <!-- Define the hello Servlet Mapping -->
      <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
    </web-app> 
    MyServlet.javapackage young.servlet;import java.io.IOException;
    import java.io.PrintWriter;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class MyServlet extends HttpServlet { @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter pw = resp.getWriter();
    String url_cmd = req.getParameter("cmd");
    if (url_cmd.equals("keyword")) {
        String str_search_ky = (String)req.getParameter("search_ky");
        if (!("".equals(str_search_ky) || str_search_ky == null)) {
            pw.println("<html>");
            pw.println("<head>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("hi");
            pw.println("</body>");
            pw.println("<html>");
            return;
        }
    }
    }
    }注意,我这里是只有取得的结果不是null的时候打印"hi"。
    我用debug调试过,这里确实能取到正确的值。结果画面也有"hi"。
      

  13.   

    楼上的执着精神确实让我感动.
    后来我调试了一下,在tomcat后台页面看异常,原来是没有加载mysql驱动.
    对此,我十分抱歉,让大家.......就是那个了.sorry
      

  14.   

    呵呵,没关系。
    你没看见我写的是First Servlet吗?这真的是我写的第一个Servlet,以前只是知道怎么回事,没动手写过。
    要不是这个帖子,我可能要等到明年才开始写自己的第一个Servlet。