Total members 11872 |It is currently Fri Jan 27, 2023 2:24 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Servlets/JSP Website search page example:
You can create a JavaBean and then call it in your Servlets/JSP
Code:



package FilmSearch
;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.http.HttpServletResponse;


/*
 * FilmSearch.java
 *
 * Created on February 1, 2007, 1:05 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

public class FilmSearch {
    
    
/** Creates a new instance of FilmSearch */
    
String Filmname;
 
    private final 
int limit=25;

    public 
String getFilmname() {
        return 
Filmname;
    }
     

    public 
void setFilmname(String Filmname) {
        
this.Filmname Filmname;
    }
    public 
String DoFilmSearch(String pagethrows IOException
    
{
         
      
String returnTable "";
      
     if(
page==null)page="1";
      
int mypage=Integer.parseInt(page);
      
int myBase=(limit*mypage)-limit;
      
int myLimit=(limit);        
        
boolean flag=false;
         try{
            try {
                Class.
forName("org.gjt.mm.mysql.Driver");
            } catch (
ClassNotFoundException ex) {
                
ex.printStackTrace();
            }
         
Connection connection =
                    
DriverManager.getConnection("jdbc:mysql://localhost/subDb?user=root&password=password");
                          
                    
Statement statement connection.createStatement();
                    
                    
String sql="Select Films.FILM_ID,FILM_NAME As 'Film name' ,COUNT(TRGMA_ID)As 'Subtitles' ,FILM_YEAR" +
                            
"  from Films Left join Trgma on(Films.FILM_ID=Trgma.FILM_ID) where FILM_NAME like('%"+Filmname+"%')" +
                            
" group by FILM_NAME,FILM_YEAR order by 'Film name' ,Subtitles limit "+myBase+","+myLimit";";
                    
ResultSet myresult= (ResultSetstatement.executeQuery(sql);
                 
                    
ResultSetMetaData myMetaData =(ResultSetMetaDatamyresult.getMetaData();
                    
int size=myMetaData.getColumnCount();   

                     
                    
returnTable"<table   >";
                   
returnTable+="<tr >";
                    for(
int i=2;i<=size-1;i++)
                    {
                    
returnTable+="<th>"+myMetaData.getColumnName(i)+"</th>";   
                    }
                    
returnTable+="</tr>";
                    
                while(
myresult.next())
                {
                   
returnTable+="<tr>";
                     
                     
flag=true;
                       
returnTable+="<td><a href=FilmTrgma.jsp?FILM_ID="+myresult.getString(1)+">"+myresult.getString(2)+"</a> ("+myresult.getString(4)+")"+"</td>";
                       
returnTable+="<td>"+myresult.getString(3)+"</td>";
                     
                
                   
returnTable+="</tr>";
                }
                   if(!
flag)
                       
returnTable+="<tr><td colspan=2> No results found </td></tr>";
                        
                        
sql="Select COUNT(FILM_ID) from Films  where FILM_NAME like('%"+Filmname+"%')";
                        
myresult= (ResultSetstatement.executeQuery(sql);
                        
myresult.next();
                        
returnTable+="<tr><td colspan=2>Page: "
                        
int count=myresult.getInt(1);
                        
int i=0;
                        while(
count>0)
                        {
                            
i++;
                            if(
i!=Integer.parseInt(page))
                            
returnTable+="<a href=FilmSearchResult.jsp?page="+i+"&&Filmname="+Filmname+">"+i+"</a>|";
                            else 
                            
returnTable+="<u>"+i+"</u>|";    
                            
count-=limit;
                        }
                    
returnTable+="</table> ";     
                   
connection.close();
                   
statement.close();
         
              } 
        
       catch (
SQLException ex) {
            
ex.printStackTrace();
        }
  
     return 
returnTable;
 
            
    }
}


 


FilmSearchBar.jsp

Code:

<%@page pageEncoding="UTF-8"%>



 
    
<%@ 
page  session="false" %>  
  <
BR><BR>
 
     
  
   <
FORM NAME="search" METHOD="POST" ACTION="FilmSearchResult.jsp">
     
           <
TABLE><Tr><th>Search for subtitles :</th>
 <
td>
    <
INPUT TYPE="TEXT" NAME="Filmname"  MAXLENGTH="100" SIZE="25" 
    <
INPUT TYPE="SUBMIT" VALUE="Search"></TD></TR></TABLE>
    
   </
FORM><BR>
   
<
script type="text/javascript">
<!--
 
search.Filmname.focus();
//-->
</script>   
  


FilmSearchResults.jsp
Code:
<%@ include file="Header.jsp" %> 
<%@
page pageEncoding="UTF-8"%>
<!
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">
    
<%@ 
page  session="false" %>
<%@ 
page import="org.gjt.mm.mysql.Driver" %>

<%@ include  
file="FilmSearchBar.jsp" %>
 <%! 
    public 
void jspInit() {
    try{
        Class.
forName("org.gjt.mm.mysql.Driver");    
       } 
       catch (
ClassNotFoundException ex) {
            
ex.printStackTrace();
        }
    }
             
 %>

<
jsp:useBean id="filmBean" class="FilmSearch.FilmSearch"  > </jsp:useBean
<
jsp:setProperty  name="filmBean" property="filmname" param="Filmname" />  
<%
     
String searchpage=(String)request.getParameter("page");
  
out.print(filmBean.DoFilmSearch(searchpage));
    
%>



<%@ include  
file="Footer.jsp" %> 
  





Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Servlets/JSP Website search page example
 A simple search page using Google AJAX Search API     -  
 How to do a simple search engine for website using PHP     -  
 Image Scroller-scrollbar-LINE_UP,LINE_DOWN-PAGE-UP-PAGE-DOWN     -  
 How does JSP differ from Servlets?!!!     -  
 material of jsp and servlets     -  
 Servlets how to upload Files     -  
 Displaying Images using servlets     -  
 help for making web pages using servlets     -  
 What is the terms applets and servlets mean?     -  
 Login using jsp,Servlets and Database following MVC     -  



Topic Tags

Servlets/JSP






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com