this is my search.jsp file <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> <%@page import="java.sql.*"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Healthy Preggers</title> <meta name="keywords" content="css templates, healthy living, diet, nutrition, fitness, web design" /> <meta name="description" content="Healthy Living - free CSS template provided by templatemo.com" /> <link href="css/templatemo_style.css" rel="stylesheet" type="text/css" /> <style type="text/css"> <!-- .style1 { color: #999999 } .style2 {color: #BF3E3E} --> </style> </head> <body> <%@include file = "header.jsp" %> <%@include file = "menu.jsp" %> <% Class.forName("oracle.jdbc.OracleDriver"); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "health", "health"); %> <body> <html> <head> </head> <body> <br><br><br><br><br><br> <form method="post" name="form1" action="viewsearch.jsp"> <table border="1" width="300" align="center" bgcolor="FFA3B3"> <tr><td colspan=2 style="font-size:12pt;color:#00000;" align="center"> <h3>Enter BMI Value</h3></td></tr> <tr><td ><b>Enter BMI </b></td> <td>: <input type="text" name="bmi_value" id="BMI_VALUE"> </td></tr> <tr><td colspan=2 align="center"> <input type="submit" name="submit" value="Search"> <input type="reset" value="Reset" name="Reset" /></td></tr> </table> </form> </body> </html> </table> </form> </body> </html> <br><br><br> <div id="templatemo_footer"> Copyright © 2012| Designed by:Fara Zati Aina</a> | For: CSC 444</div> </center></body> </html> this is my searchview.jsp file where it does the process <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> <%@page import="java.sql.*"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Healthy Preggers</title> <meta name="keywords" content="css templates, healthy living, diet, nutrition, fitness, web design" /> <meta name="description" content="Healthy Living - free CSS template provided by templatemo.com" /> <link href="css/templatemo_style.css" rel="stylesheet" type="text/css" /> <style type="text/css"> <!-- .style1 { color: #999999 } .style2 {color: #BF3E3E} --> </style> </head> <body> <%@include file = "header.jsp" %> <%@include file = "menu.jsp" %> <% Class.forName("oracle.jdbc.OracleDriver"); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "health", "health"); %> <body> <html> <head> </head> <body> <br><br><br><br><br><br> <form method="post" name="form1" action="viewsearch.jsp"> <table border="1" width="300" align="center" bgcolor="FFA3B3"> <tr><td colspan=2 style="font-size:12pt;color:#00000;" align="center"> <h3>Enter BMI Value</h3></td></tr> <tr><td ><b>Enter BMI </b></td> <td>: <input type="text" name="bmi_value" id="BMI_VALUE"> </td></tr> <tr><td colspan=2 align="center"> <input type="submit" name="submit" value="Search"> <input type="reset" value="Reset" name="Reset" /></td></tr> </table> </form> </body> </html> </table> </form> </body> </html> <br><br><br> <div id="templatemo_footer"> Copyright © 2012| Designed by:Fara Zati Aina</a> | For: CSC 444</div> </center></body> </html> this is my searchview2.jsp file where it display the result <%-- Document : viewSearch2 Created on : Jun 18, 2012, 8:21:02 PM Author : Asus --%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> <%@page import="java.sql.*"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Healthy Preggers</title> <meta name="keywords" content="css templates, healthy living, diet, nutrition, fitness, web design" /> <meta name="description" content="Healthy Living - free CSS template provided by templatemo.com" /> <link href="css/templatemo_style.css" rel="stylesheet" type="text/css" /> <style type="text/css"> <!-- .style1 { color: #999999 } .style2 {color: #BF3E3E} --> </style> </head> <body> <%@include file = "header.jsp" %> <%@include file = "menu.jsp" %> <% Class.forName("oracle.jdbc.OracleDriver"); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "health", "health"); %> <body> <html> <body> <h1 align="left">VIEW SEARCH</h1> <% Statement st=con.createStatement(); ResultSet rs3=st.executeQuery("SELECT M.MEMBER_ID, M.FNAME,M.LNAME,M.AGE,B.HEIGHT,B.WEIGHT, B.BMI_VALUE FROM MEMBER M , BMI B WHERE M.MEMBER_ID=B.MEMBER_ID"); %> <table align="center" border="1"> <tr bgcolor="ACFA58"> <th class="listingContent" witdth="10">No</th> <th class="listingContent" width="100">ID</th> <th class="listingContent" width="1000">FIRST NAME</th> <th class="listingContent" width="1000">LASTNAME</th> <th class="listingContent" width="1000">AGE</th> <th class="listingContent" width="1000">HEIGHT</th> <th class="listingContent" width="1000">WEIGHT</th> <th class="listingContent" width="300">BMI VALUE</th> </tr> <% String data=request.getParameter("bmi_value"); if(data != "bmi_value"){ %> <script language="javascript"> alert("There is no data."); window.location.href = "searchBMI.jsp"; </script> <% } else{ %> <% int y= 1; while(rs3.next()) { %> <tr bgcolor="D0F5A9" onclick="form1"> <td class="listingContent" align="center"><%= y%></td> <td class="listingContent" width="10" align="center"><%= rs3.getString("member_id") %></td> <td class="listingContent" width="10" align="center"><%= rs3.getString("fname") %></td> <td class="listingContent" width="10" align="center"><%= rs3.getString("lname")%></td> <td class="listingContent" width="10" align="center"><%= rs3.getString("age")%></td> <td class="listingContent" width="10" align="center"><%= rs3.getString("height")%></td> <td class="listingContent" width="10" align="center"><%= rs3.getString("weight")%></td> <td class="listingContent" width="10" align="center"><%= rs3.getString("bmi_value")%></td> </tr> <% y++; }%> } </table> </body> </html> i suppose to get this table, but it shows wrong value .i want it to be the same with what i have entered in the search field. let say i entered 17, so i want the data of user who have 17 as their bmi value to appear. help me please. i don't know which line i have to add or change my code. thank you :)
Here is a simple JSP search application where user is allowed to enter BMI value and display the results accordingly.
1)search.jsp:
<html> <head> </head> <body> <br><br><br><br><br><br> <form method="post" name="frm" action="..//search"> <table border="0" width="300" align="center" bgcolor="#CDFFFF"> <tr><td colspan=2 style="font-size:12pt;color:#00000;" align="center"> <h3>Search Form</h3></td></tr> <tr><td ><b>Enter BMI</b></td> <td>: <input type="text" name="bmi" id="bmi"> </td></tr> <tr><td colspan=2 align="center"> <input type="submit" name="submit" value="Search"></td></tr> </table> </form> </body> </html>
2) Search.java:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.sql.*; import java.sql.*; import java.util.*; public class Search extends HttpServlet{ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); System.out.println("MySQL Connect Example."); Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "register"; String driver = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "root"; Statement st; try { Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url+dbName,userName,password); System.out.println("Connected to the database"); String bmi = request.getParameter("bmi"); String query = "select * from item where bmi='"+bmi+"'"; st = conn.createStatement(); ResultSet rs = st.executeQuery(query); out.println("<table border='1'><tr><th>Member ID</th><th>First Name</th><th>Last Name</th><th>Age</th><th>BMI Value</th></tr>"); while(rs.next()){ out.println("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td><td>"+rs.getString(5)+"</td></tr>"); } out.println("</table>"); conn.close(); System.out.println("Disconnected from database"); } catch (Exception e) { e.printStackTrace(); } } }
3)web.xml:
<servlet-name>Search</servlet-name> <servlet-class>Search</servlet-class> </servlet> <servlet-mapping> <servlet-name>Search</servlet-name> <url-pattern>/search</url-pattern> </servlet-mapping>