简体   繁体   English

Jsp和Servlet处理

[英]Jsp and Servlet Handling

I have one jsp page and one servlet, In JSP page I have two forms, each of select input type ie,select class and select student. 我有一个jsp页面和一个servlet,在JSP页面中,我有两种形式,分别是选择输入类型,即选择类和选择学生。 When I select class from the select dropdown,by onchange event it goes to abcServlet,retrieves the class from the servlet and it forwards to jsp page goes to next form in the jsp page ie, select student,it submits again to the same servlet,from their servlet forwards to same jsp page and students are retrieved from the servlet and Students are displayed in the select student dropdown correctly according to the class selected but my problem is when I select a student in select student dropdown, Class value is changing to null (I think when first time servlet forwards to jsp page, class is retrieved properly from the abcServlet and when I select student from the next drop down,second time whole jsp is forwarded, so class value is becoming null for the second time) I want my class to be remain same when I select student from the student dropdown after selecting class,How can I achieve that? 当我从select下拉列表中选择类时,通过onchange事件,它会转到abcServlet,从servlet中检索该类,然后转发到jsp页,并转到jsp页中的下一个表单,即select student,它再次提交到同一servlet,从他们的servlet转发到相同的jsp页面,并且从servlet中检索到学生,并且根据所选班级在“学生选择”下拉列表中正确显示了学生,但是我的问题是当我在“学生选择”下拉列表中选择一个学生时,“班级”值更改为null (我认为当第一次将servlet转发到jsp页面时,可以从abcServlet中正确检索类,并且当我从下一个下拉列表中选择Student时,第二次将整个jsp转发,因此类值第二次变为null)选择班级后,从学生下拉列表中选择学生时,我的班级保持不变,如何实现?

StudentFee.java StudentFee.java

        String sname[]={};
        ResultSet rs;
        int no_stdnts=0,admfee=0;
        String clas;
        clas=request.getParameter("class");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost/XYZ","XYZ","XYZ");
        Statement st;
        st=(Statement)con.createStatement();
        rs=st.executeQuery("select count(sid) from students where class='"+clas+"'");
        while(rs.next())
        {
            no_stdnts=rs.getInt(1);
        }
        String snames[]=new String[no_stdnts];
        String stdnt=request.getParameter("student");

        request.setAttribute("snames", snames);
        request.setAttribute("no_stdnts", no_stdnts);
        request.setAttribute("cls", clas);

        RequestDispatcher rd=request.getRequestDispatcher("studentfee.jsp");
        rd.forward(request,response);

studentfee.jsp studentfee.jsp

<form  name="sclass" id="sclass" action="StudentFee" method="post">
            <br><br>
            <label style="font-size: 20px; word-spacing: 4px;"><b>Select Class : </b></label>
            <select name="class" id="clas" onchange="Javascript:sclass.submit()">
                        <option value="Select" style="visibility:hidden;"><% String cls=(String)request.getAttribute("cls");if(cls!=null)out.print(cls);else out.print("Select");%></option>
                        <option value="Nursery">Nursery</option>
                        <option value="LKG">LKG</option>
                        <option value="UKG">UKG</option>
                        <option value="I">I</option>
                        <option value="II">II</option>
                        <option value="III">III</option>
                        <option value="IV">IV</option>
                        <option value="V" >V</option>
                        <option value="VI">VI</option>
                        <option value="VII">VII</option>
                        <option value="VIII">VIII</option>
                        <option value="IX">IX</option>
                        <option value="X">X</option>
            </select>                
        </form>
        </td></tr>
        <tr><td>
        <form  name="sname" id="sname" action="StudentFee" method="post">
            <br><br>
            <label style="font-size: 20px; word-spacing: 4px;"><b>Select Student : </b></label>
            <select name="student" id="clas" onchange="Javascript:sname.submit()">

                        <%

                        try
                        {
                            response.setContentType("text/html");
                            int no_stdnts=(Integer)request.getAttribute("no_stdnts");
                            String snames[]=new String[no_stdnts];
                            snames=(String[])request.getAttribute("snames");
                            for(int i=0;i<no_stdnts;i++)
                            { %>
                            <option>
                            <% out.print(snames[i]);
                            }   
                        }
                        catch(Exception e)
                        {
                            e.getMessage();
                        }
                        %>
                        </option>
            </select>                
        </form>

It sounds like there's a session implicit in all this. 听起来这隐含着一个会话。 You have two operations, and you want the second one to remember the result of the first. 您有两个操作,并且希望第二个操作记住第一个操作的结果。

You can maintain related values in session. 您可以在会话中维护相关值。 If you don't find one, that means the query has never been run. 如果找不到,则表明该查询从未运行过。 If you do, return it and display it. 如果这样做,将其退回并显示。

The first thing I noticed are same id values for both select fields. 我注意到的第一件事是两个select字段的id值相同。 Why? 为什么? It's better to use differnt values and use same values for id and name for each select . 最好使用不同的值,并为每个select idname使用相同的值。

The second problem is, that you use two different forms without to handle state. 第二个问题是,您使用两种不同的形式而不处理状态。 If you submit one form then the POST operation contains only parameters for this form's input fields. 如果您提交一种表单,则POST操作仅包含该表单输入字段的参数。 Eg if you submit form sname then the POST request contains only parameter student and no parameter class . 例如,如果您提交表单sname则POST请求仅包含参数student而没有参数class

You have to maintain the state of previous selections in some kind. 您必须以某种方式维护先前选择的状态。 There are several alternatives to achieve this. 有几种替代方法可以实现此目的。

  1. You can store the previous selections in your session. 您可以将先前的选择存储在会话中。 In your JSP you can then first try to restore the selection from the session. 然后,您可以在JSP中首先尝试从会话中还原选择。 If there is no value stored in session then show the default value. 如果会话中没有存储任何值,则显示默认值。 In this scenario you can access the state of previous selections in later requests. 在这种情况下,您可以在以后的请求中访问先前选择的状态。
  2. If you use only one form for both input fields, then each form submit will contain both parameters student and class . 如果您对两个输入字段仅使用一种表单,则每个表单提交都将包含参数studentclass In that case your servlet always gets the previous selection and you don't have to use server side state with a session. 在这种情况下,您的servlet总是会获得先前的选择,而不必在会话中使用服务器端状态。
  3. If you still want to use two different forms, then place a hidden field class within the second form and initialize its value to the previous selection. 如果仍要使用两种不同的形式,则将隐藏字段class放在第二种形式中,并将其值初始化为先前的选择。 If the user submits the second form your servlet will get the additional parameter class as in 2nd scenario. 如果用户提交第二种形式,则您的servlet将获得第二种情况下的其他参数class

The difference between 2. and 3. is that in 2. the user is able to change both selection at once. 2.和3.之间的区别在于,在2.中,用户可以一次更改两个选择。

If you got an understanding how to manage such state in a traditional way you can proceed with using AJAX where you don't submit and reload the whole page but only some small parts of it. 如果您了解如何以传统方式管理此类状态,则可以继续使用AJAX,而无需提交和重新加载整个页面,而只需提交其中的一小部分。 There are several tutorials out there, just one Google search away. 那里有几本教程,仅需一次Google搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM