View Single Post
Old 06-28-2012, 12:09 PM   PM User | #1
brood_snow
New to the CF scene

 
Join Date: Jun 2012
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
brood_snow is an unknown quantity at this point
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.

Last edited by brood_snow; 06-28-2012 at 01:36 PM.. Reason: more precise
brood_snow is offline   Reply With Quote