CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Processing form's element(s) in JSP (http://www.codingforums.com/showthread.php?t=266533)

brood_snow 06-28-2012 12:09 PM

Processing form's element(s) in JSP
 
I have a HTML form in JSP page, and in the <header> I have a JavaScript validation. The user must enter one field: name or id or year, and a java file will search the student in database by name or by id or by year. The JavaScript alerts when no field is filled and performs the **action** if one field is filled.

Code:

<html><head>.....</head>
        <body bgcolor=#ADD8E6><center>
                       
        <form action="FoundStudents.jsp" method="post" name="entry2" onSubmit="return validateSea()">
        <input type="hidden" value="list" name="seek_stud">

...........................................................................................

The problem is I want to process the parameter which I receive in FoundStudents.jsp: If I get the year, I look in DB which student(s) are in that year and display all that student(s)' data(do that in a java file). How could I do that in FoundStudents.Jsp without checking again which field is filled(I've done that in JavaScript from **SearchStudent.jsp**). I mean the FoundStudents.jsp calls a method in the java file for searching and displaying.

I tried by now with the **input hidden** that worked, but that is for more forms. I have only 1.

**FoundStudent.jsp**

Code:

<%@page import="stud.diploma.students.StudentsManager"%>
    <%@page import="stud.diploma.students.Student"%>
    <%@page import="java.util.ArrayList"%>
    <%@page import="stud.diploma.database.ConnectionsManager"%>
   
    <%@ page language="java" import="java.sql.*, java.lang.*" %>
   
    <%
    Student search = null;
    if(request.getParameter("seek_stud") != null){
                         
                    //reading params from the SearchStudent form
                    String name = request.getParameter("name");
                    String year_prime = request.getParameter("year");
            int year, id;
                   
                    try{
                            year = Integer.parseInt(year_prime);
                    }catch(Exception e1){
                            year = 0;
                    }
                   
                String id_prime = request.getParameter("id");
                try{
                        id = Integer.parseInt("id");
                }catch(Exception e2){
                        id = 0;
                }
               
               
                if(name.length() != 0){
                    search = StudentsManager.getInstance().studByName(name);
                }
               
                if(year > 0){
                  search = StudentsManager.getInstance().studByYear(year);
                              }
               
                if(id > 0){
                  search = StudentsManager.getInstance().studById(id);
                }   
   
                if(search != null){
    %>
   
    <html>
                    <body bgcolor=#4AA02C>
                    <center>
                            <h2>Student's data</h2>
                            <table border="1" cellspacing="1" cellpadding="8" bgcolor= #EBDDE2>
                            <tr>
                                    <td bgcolor= #FF9966><b>ID</b></td>
                                    <td bgcolor= #FF9966><b>Name</b></td>
                                    <td bgcolor= #FF9966><b>Year</b></td>
                            </tr>
                                <tr>
                                      <td><%= search.getId()%></td>
                                      <td><%= search.getName()%></td>
                                      <td><%= search.getYear()%></td>
                                    </tr>
                            </table>
                            </center>
                    </body>
            </html>
    <%}else{%>
           
    <%
    String redirectURL = "MainMenu.html";
    response.sendRedirect(redirectURL);
    %>
   
        <%}%>
    <%}%>

This **FoundStudent.jsp** is for the version of multiple forms (using hidden input) that worked. (the javascript test was just a little bit different, I typed it insted of what I had in the beginning)

It searched by name and by year only. Didn't search by ID (I had exception here `
Code:

<td><%= search.getId()%></td>
` I'm still trying to see how to deal with it. ID is a AUTO_INCREMENT PRIMARY KEY)

Lines like : search = StudentsManager.getInstance().studByName(name);
**Search** is a Student type object. (Object Student is creaded in a java file)

**StudentsManager** is a java class that receives calls to it's methods from JSP. getInstance() creates an instance of StudentsManager. Method **studByName(name)** receives the parameter **name** from the form and searches it in the database.

brood_snow 07-01-2012 03:15 PM

I changed the (java)script to:

Code:

<script language="javascript">
                        function validateSea(){
                                if(document.entry2.name.value != ''){
                                        return true;
                                }
                               
                                else
                                    if(document.entry2.year.value != ''){
                                          return true;
                                    }
                               
                                alert('Please fill one field.');
                                return false;       
                    }
          </script>

And in FoundStudents.jsp i test if user entered name OR year:

Code:

if((request.getParameter("year") != null)||(request.getParameter("name") != null)){
                     
                //reading params from the SearchStudent form
              //.......................
}

I'm not sure I did the best thing in FoundStudents, but it works.


All times are GMT +1. The time now is 10:08 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.