Change html with servlet to jsp - java

I'm having a bit of trouble with changing html pages to jsp with corresponding changes to the servlet. Usually I have not had an issue with this, but I am now trying to access the database and display tables.
Firstly, the html, corresponding servlet and dao which all works fine.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Bids</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<img src="Images/AllBids_Logo.gif" width="295" height="42">
<form action="controller" method="POST">
<table width="494" height="63" border="1">
<tr>
<td width="70" height="28" bgcolor="#CCCCCC">
<select name="Bidders" >
<option value="NULL">--Select a Bidder--</option>
<option value="M100">Anne Deems</option>
<option value="M200">Joe Black</option>
<option value="M300">Henry Brown</option>
<option value="M400">Sam Pink</option>
<option value="M500">Eva Blue</option>
<option value="M600">George Grey</option>
<option value="M700">James Green</option>
<option value="M800">Sally Orange</option>
</select>
</td>
<td width="150" bgcolor="#CCCCCC">
<select name="Items" >
<option value="NULL">--Select An Item to Bid--</option>
<option value="P100">Acer Pentium 5 Laptop</option>
<option value="P200">Canon Inkjet Printer</option>
<option value="P300">Pioneer Blue Ray Recorder</option>
<option value="P400">20 cm ViewSonic LCD Color Monitor</option>
</select>
</td>
</tr>
<tr bgcolor="#999999">
<td height="27" >
<input type="submit" name="Submit" value="Submit Bid">
</td>
<td bgcolor="#CCCCCC">
<input type="hidden" name="action" value="bid">
Enter Your Bid <input type="text" name="bidValue" size="10">
</td>
</tr>
</table>
<p> </p>
</form>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>View Bids</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<img src="Images/AllBids_Logo.gif" width="295" height="42" alt="Logo">
<form action="controller" method="POST">
<table width="494" height="63" border="1">
<tr>
<td height="28" bgcolor="#CCCCCC">
<select name="Items" size="1">
<option value="NULL">--Select An Item to View Bids--</option>
<option value="P100">Acer Pentium 5 Laptop</option>
<option value="P200">Canon Inkjet Printer</option>
<option value="P300">Pioneer Blue Ray Recorder</option>
<option value="P400">20 cm ViewSonic LCD Color Monitor</option>
</select>
<input type="submit" name="Submit" value="Display Bids">
</td>
</tr>
<tr bgcolor="#999999">
<td height="27">
<input name="action" type="hidden" id="action" value="viewBids">
</td>
</tr>
</table>
</form>
</body>
</html>
This is the part of the servlet used, TableFormatter is a class located within a jar file in the Libraries package.
else if(action.equals("bid")){
AuctionDAO dao = new AuctionDAO(ds);
String itemID = request.getParameter("Items");
String bidderID = request.getParameter("Bidders");
double bidAmount = Double.parseDouble(request.getParameter("bidValue"));
try{
boolean bidRecorded = dao.regBid(itemID,bidderID,bidAmount);
if(bidRecorded){
out.println(formatPage("Your Bid for " + bidAmount + " was recorded"));
}
else {
out.println(formatPage("Your Bid could not be registered"));
}
}catch(Exception ex){
out.println(ex.getMessage());
}// end catch
}// end if
else if(action.equals("viewBids")){
// AuctionDAO dao = new AuctionDAO(ds);
String itemID = request.getParameter("Items");
try{
ResultSet rs = dao.viewBids(itemID);
out.print(TableFormatter.getData(rs));
}catch(Exception ex){
out.print(ex.getMessage());
}// end catch
}// end if
private String formatPage(String message){
StringBuffer output = new StringBuffer();
output.append("<html><head><title>Auction Admin</title></head><body>");
output.append("<h1>" + message + "</h1>");
output.append("</body>");
output.append("</body></html>");
return output.toString();
}
and then, the dao
public boolean regBid(String itemId, String bidderId, double amount)throws SQLException{
int recsInserted = 0;
regBidQuery();
regBidPS.setString(1, bidderId);
regBidPS.setString(2,itemId );
regBidPS.setDouble(3, amount);
recsInserted = regBidPS.executeUpdate();
if(recsInserted == 1){
return true;
}
else {
return false;
}
}
private void regBidQuery()throws SQLException{
regBidPS = con.prepareStatement("INSERT INTO USER1.TBids " +
"VALUES(?,?,?)");
}
Now, All of this works perfectly fine. My jsp pages even work with the new servlet code, but I can't seem to get how to access the TableFormatter class or the regBids() method.
Jsp's:
<%#page import="beans.Member"%>
<%#page import="java.util.List"%>
<%#page import="beans.Item"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bids</title>
</head>
<body>
<img src="Images/AllBids_Logo.gif" width="295" height="42">
<form action="Controller" method="POST">
<table border="1">
<tbody>
<tr>
<td>
<select name="members">
<option value="NULL" selected="true">-- Select A Member --</option>
<% List<Member> members = (List<Member>) session.getAttribute("members");
for (Member member : members){ %>
<option value=<%= member.getMemberId()%> > <%= member.getMemberName()%></option>
<% } //end for %>
</select>
</td>
<td>
<select name="items">
<option value="NULL" selected="true">-- Select An Item To View Bids --</option>
<% List<Item> items = (List<Item>) session.getAttribute("items");
for (Item item : items){ %>
<option value=<%= item.getItemId()%> > <%= item.getItemName()%></option>
<% } //end for %>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit Bid"/>
</td>
<td>
Enter Your Bid <input type="text" name="bidValue" size="10">
</td>
</tr>
</tbody>
</table>
<input type="hidden" name="action" value="bid_view"/>
</form>
</body>
</html>
<%#page import="java.util.List"%>
<%#page import="beans.Item"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>View Bids</title>
</head>
<body>
<img src="Images/AllBids_Logo.gif" width="295" height="42" alt="Logo">
<form action="Controller" method="POST">
<table border="1">
<tbody>
<tr>
<td>
<select name="items">
<option value="NULL" selected="true">-- Select An Item To View Bids --</option>
<% List<Item> items = (List<Item>) session.getAttribute("items");
for (Item item : items){ %>
<option value=<%= item.getItemId()%> > <%= item.getItemName()%></option>
<% } //end for %>
</select>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<input type="submit" value="Display Bids"/>
</td>
</tr>
</tbody>
</table>
<input type="hidden" name="action" value="viewbids_view"/>
</form>
</body>
</html>
And this is how I am now running the servlet code - as you can see, the code I have generates the same content as the original html pages, it's just accessing the db and displaying the table and messages that I can't figure out.
if(action.equals("viewbids_view")) {
HttpSession session = request.getSession();
session.setAttribute("items", dao.getItems());
response.sendRedirect("ItemBids.jsp");
}
else if(action.equals("bid_view")) {
HttpSession session = request.getSession();
session.setAttribute("items", dao.getItems());
session.setAttribute("members", dao.getMembers());
response.sendRedirect("Bids.jsp");
}
I'm pretty sure this is the problem, but where do I look to resolve the issue?

Related

Non English Data Input From Java Web Application in Netbeans 8.0.2

My Connection String
Class.forName("com.mysql.jdbc.Driver");
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/exam_system?useSSL=false&useUnicode=true&characterEncoding=utf8","root","Password");
My JSP File
<%#page import="java.util.ArrayList"%>
<jsp:useBean id="pDAO" class="myPackage.DatabaseClass" scope="page"/>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style-backend.css">
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="utf-8" >
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<label Style="color: black"> ছন্দে</label>
<input type="hidden" name="pgprt" value="4">
<select name="coursename" class="text">
<%
ArrayList list1=pDAO.getAllCourses();
for(int i=0;i<list1.size();i=i+2){
%>
<option value="<%=list1.get(i)%>"><%=list1.get(i)%></option>
<%
}
%>
</select>
<input type="submit" value="Show" class="form-button">
</form>
</div>
<div class="panel form-style-6" style="max-width: 420!important;float: right">
<form action="controller.jsp" >
<div class="title">Add New Question</div>
<table Style="color: black;font-color: black">
<tr>
<td><label Style="color: black">Course Name</label></td>
<td colspan="3">
<select name="coursename" class="text">
<%
ArrayList list=pDAO.getAllCourses();
for(int i=0;i<list.size();i=i+2){
%>
<option value="<%=list.get(i)%>"><%=list.get(i)%></option>
<%
} request.setCharacterEncoding("UTF-8");
%>
</select>
</td>
</tr>
<tr>
<td><label Style="color: black">Your Question:</label></td>
<td colspan="4"><input type="text" name="question" class="text" placeholder="Type your question here" style="width: 420px;" ></td><br>
</tr>
<tr>
<td><label Style="color: black">Options</label></td>
<td><input type="text" name="opt1" class="text" placeholder="First Option" style="width: 130px;" ></td>
<td><input type="text" name="opt2" class="text" placeholder="Second Option" style="width: 130px;" ></td>
<td><input type="text" name="opt3" class="text" placeholder="Third Option" style="width: 130px;" ></td>
<td><input type="text" name="opt4" class="text" placeholder="Fourth Option" style="width: 130px;" ></td>
</tr>
<tr>
<td><label>Correct Answer</label></td>
<td colspan="4"><center><input type="text" name="correct" class="text" placeholder="Correct Answer" style="width: 130px;" ></center></td>
<tr>
<td colspan="5"><input type="hidden" name="page" value="questions">
<input type="hidden" name="operation" value="addnew">
<center><input type="submit" class="form-button" value="Add" name="submit"></center></td>
When Inputting Non-English Data It is Not Inserting Correctly in MySQL Database. But
I changed Database Pattern And Test By Passing Non-English Data In String And It Works Perfectly.
INSERT INTO `exam_system`.`contact`
(`Name`,
`Email`,
`Message`)
VALUES
('<{Name:fsfsd }>',
'<{Email:sdfsdf }>',
'<{Message: 1. চর্যাপদ কোন ছন্দে লেখা? }');
Before Answering I tried
Net Bean Font Change
Tried Change String Data To Byte
And Rest
Change Are in the symbol Code.
When Accepting Parameter or User Value Convert That Value Into Char .
String crr=request.getParameter("correct");
byte[] bytesCrr = crr.getBytes(StandardCharsets.ISO_8859_1);
crr = new String(bytesCrr, StandardCharsets.UTF_8);

How to get parameter name in case of spring security?

I want to retrieve the parameters sent through the form login.jsp in the controller..but I am getting the error-"Request parameter is not present". My code for login.jsp is as follows:-
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring Security</title>
</head>
<body>
<center>
<h1>Welcome to Spring Security Learning</h1>
<c:if test="${not empty SPRING_SECURITY_LAST_EXCEPTION}">
<font color="red">
Your login attempt was not successful due to <br/><br/>
<c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/>.
</font>
</c:if>
<div style="text-align: center; padding: 30px; border: 1px solid; width: 250px;">
<form method="post"
action='j_spring_security_check'>
<table>
<tr>
<td colspan="2" style="color: red">${message}</td>
</tr>
<tr>
<td>User Name:</td>
<td><input type="text" name="j_username" id="uname"/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="j_password" /></td>
</tr>
<tr>
<td colspan="1"><input type="submit" value="Login" /></td>
<td colspan="1"><input name="reset" type="reset" /></td>
</tr>
</table>
</form>
</div>
</center>
</body>
</html>
The code for controller is as follows:-
#RequestMapping("/search")
public ModelAndView search(HttpSession session2,#RequestParam("j_username") String name) {
session2.setAttribute("name", name);
Session session=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory().openSession();
String hql = "SELECT E.firstName FROM User E";
Query query = session.createQuery(hql);
List results = query.list();
//System.out.println(results.get(1).getClass().getFields());
HashMap model = new HashMap();
model.put("users", results);
return new ModelAndView("search", model);
}
Please help..thanx in advance..

The type java.lang.Charsequence cannot be resolved. It is indirectly referenced from required .class file

<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic"%>
<%# page import="de.fhg.fokus.hss.db.model.*, java.util.* " %>
<jsp:useBean id="resultList" type="java.util.List" scope="request"></jsp:useBean>
<jsp:useBean id="maxPages" type="java.lang.String" scope="request"></jsp:useBean>
<jsp:useBean id="currentPage" type="java.lang.String" scope="request"></jsp:useBean>
<jsp:useBean id="rowPerPage" type="java.lang.String" scope="request"></jsp:useBean>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" type="text/css" href="/hss.web.console/style/fokus_ngni.css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><bean:message key="result.title" /></title>
<script type="text/javascript" language="JavaScript">
function submitForm(pageId){
document.IMSU_SearchForm.crtPage.value = pageId;
document.IMSU_SearchForm.submit();
}
function rowsPerPageChanged(){
document.IMSU_SearchForm.crtPage.value = 1;
document.IMSU_SearchForm.submit();
}
</script>
</head>
<body>
<table id="title-table" align="center" weight="100%" >
<tr>
<td align="center">
<h1> IMS Subscription - Search Results </h1>
<br/><br/>
</td>
</tr>
</table> <!-- title-table -->
<table id="main-table" align="center" valign="middle" >
<tr>
<td>
<table id="result-table" class="as" width="600" border="0" cellspacing="1" align="center" style="border:2px solid #FF6600;">
<tr class="header">
<td class="header"> ID </td>
<td class="header"> Name </td>
<td class="header"> S-CSCF Name </td>
<td class="header"> Diameter Name </td>
</tr>
<%
if (resultList != null && resultList.size() > 0){
IMSU imsu;
int idx = 0;
Iterator it = resultList.iterator();
while (it.hasNext()){
imsu = (IMSU) it.next();
%>
<tr class="<%= idx % 2 == 0 ? "even" : "odd" %>">
<td>
<%= imsu.getId() %>
</td>
<td>
<a href="/hss.web.console/IMSU_Load.do?id=<%= imsu.getId() %>">
<%= imsu.getName() %>
</a>
</td>
<td>
<%= imsu.getScscf_name() %>
</td>
<td>
<%= imsu.getDiameter_name() %>
</td>
</tr>
<%
idx++;
} //while
} // if
else{
%>
<tr>
<td>
<bean:message key="result.emptyResultSet" />
</td>
</tr>
<%
}
%>
</table> <!-- result-table -->
</td>
</tr>
<tr>
<td colspan="3" class="header">
<html:form action="/IMSU_Search">
<table id="rows-table" align="center">
<tr>
<td>
<%
int length = Integer.parseInt(maxPages) + 1;
int cPage = Integer.parseInt(currentPage) + 1;
for (int iy = 1; iy < length; iy++) {
if (cPage != iy) {
%>
<%=iy%>
<%
} else {
%>
<font style="color:#FF0000;font-weight: 600;">
<%=String.valueOf(iy)%>
</font>
<% }
}
%>
</td>
<td>
<bean:message key="result.rowsPerPage" /><br>
<html:hidden property="crtPage"></html:hidden>
<html:select property="rowsPerPage" onchange="javascript:rowsPerPageChanged();">
<option value="20"
<%= rowPerPage.equals("20") ? "selected" : "" %> >20 </option>
<option value="30"
<%= rowPerPage.equals("30") ? "selected" : "" %> >30 </option>
<option value="50"
<%= rowPerPage.equals("50") ? "selected" : "" %> >50</option>
<option value="100"
<%= rowPerPage.equals("100") ? "selected" : "" %> >100</option>
</html:select>
</td>
</tr>
</table> <!-- rows-table -->
</html:form>
</td>
</tr>
</table> <!-- main-table -->
</body>
</html>
I am trying to run a appliaction, I am accessing the application through a web interface but at some part I am getting the following error:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 116 in the jsp file: /pages/user/imsu_search_result.jsp
Generated servlet error:
The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063)
org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:263)
org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386)
org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:318)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.
Please find the attached jsp file
Please provide your suggestion to resolve the issue.
Thanks
1) Check your compiler and JVM versions as I believe they mismatch OR
2) Compile your all classes with newer version of 1.8 or
3) downgrade to Java 1.7

How to populate a hidden HTML table with the result from the database?

I have a JSP called create-account.jsp which collects a customer's details and sends the data to a Servlet. The Servlet inserts the customer's data into the database, queries the database add sets the results as request scoped attributes and the dispatches to create-account.jsp(same jsp). Now the inserted details must appear in a hidden html table in the jsp. How do I do it? Can any one help?
create-account.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Create New Account</title>
<style>
a {
text-decoration: none;
}
body {
border-color: azure;
}
</style>
</head>
<body>
<p align="right">Hi, ${sessionScope.user.userName} logout</p>
<h2 align="center">Create New Account</h2>
<form action="${pageContext.request.contextPath}/create.do" method="post">
<table border="1" align="center">
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>DOB:(yyyy-mm-dd)</td>
<td><input type="text" name="dob"></td>
</tr>
<tr>
<td>Balance:</td>
<td><input type="text" name="balance"></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="Create" name="create">
</td>
</tr>
</table>
</form>
<p align="center" style="display: none">${requestScope.result} has been successfully inserted into the table</p>
<table id="hiddenTable" style="display: none" border="1">
<tr>
<td>
</td>
</tr>
</table>
</body>
</html>
Use
style="visibility: hidden;"
or
style="display:none;"
to hide the table.
for more info have a look at below links
CSS Display and Visibility
Show/Hide div javascript
How to show the result in table?
sample code:
Servlet:
public class Model{
private String name;
private String dob;
private double balance;
// getter and setter
}
...
List<Model> list = new ArrayList<Model>();
//add the data in list
request.setAttribute("list",list);
JSP:
<c:if test="${not empty list}">
<c:forEach var="ob" items="${list}">
<tr>
<td><c:out value="${ob.name}"/></td>
<td><c:out value="${ob.dob}"/></td>
<td><c:out value="${ob.balance}"/></td>
</tr>
</c:forEach>
</c:if>
For complete code have a look at jsp iterate over list

how to display the names in a jsp page according to name

i have a page editpatient.jsp which includes a page patientlist.jsp. when you run editpatient.jsp then it displays all the records present in the database.I have a dropdown and also a search field to specify searches. So when i run editpatient.jsp then it displays all the records in the manner it is stored in DB. So i wanted to sort it according to name and display.So please tell me how to do the same. when you hit the name or email or city then it will sort accordingly
patientlist.jsp
<%# page import="java.util.*" %>
<%# page import="java.sql.*" %>
<%# page import="DB.*" %>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.evenRow{
height: 50px;
background-color: white;
text-transform: none;
text-shadow: none;
color: black;
}
.evenRow:hover
{
background-color: #C2FEF0;
}
.row{
height: 50px;
background-color: #E4E6E6;
text-transform: none;
text-shadow: none;
color: black;
}
.row:hover {
background-color: #C2FEF0;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<table style="border-collapse: collapse;overflow-x: scroll; width:97%">
<tr style="background-color:grey;height:50px">
<th style="min-width: 100px">
NAME
</th>
<th style="min-width: 100px">
CITY
</th>
<th style="min-width: 100px">
LAST VISIT
</th>
<th style="min-width: 100px">
MOBILE
</th>
<th style="min-width: 100px">
EMAIL
</th>
<th style="min-width: 100px">
STATUS
</th>
<th style="min-width: 100px">
VIEW
</th>
<th style="min-width: 100px">
EDIT
</th>
</tr>
<%
DataBaseConnection db = new DataBaseConnection();
Connection con = db.connet();
PreparedStatement pt = con
.prepareStatement("select * from Patient");
ResultSet rs = pt.executeQuery();
String searchBy = request.getParameter("searchBy");
String searchElement = request.getParameter("searchElement");
int count = 0;
int index = -1;
boolean name = false;
if ("city".equalsIgnoreCase(searchBy))
index = 9;//change the index to the index of the city
else if ("firstName".equalsIgnoreCase(searchBy))
index = 1;
else if ("lastName".equalsIgnoreCase(searchBy))
index = 2;
else if ("name".equalsIgnoreCase(searchBy)) {
index = 1;
name = true;
}
while (rs.next()) {
if (searchElement == null
|| ((searchElement.equals(rs.getString(index)) && !name) || (name && searchElement
.equalsIgnoreCase(rs.getString(index) + " "
+ rs.getString(index + 1))))) {
if (count++ % 2 == 0) {
%>
<tr class="evenRow" >
<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>
<td>
<%=rs.getString(6)%>
</td>
<td>
<form action="getPatientDetails.jsp"><input type="hidden" name="hidden" value="<%=count%>"/><input type="submit" value="view"></form>
</td>
<td>
<a onclick="renderEdit(<%out.println("edit");%>)"><%
out.println("edit");
%></a>
</td>
</tr>
<%
} else {
%>
<tr class="row">
<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>
<td>
<%=rs.getString(6)%>
</td>
<td>
<a onclick="renderView(<%out.println("view");%>)"><%
out.println("view");
%></a>
</td>
<td>
<a onclick="renderEdit(<%out.println("edit");%>)"><%
out.println("edit");
%></a>
</td>
</tr>
<%
}
}
}
%>
</table>
</body>
</html>
editpatient.jsp
<%# page import="java.util.*" %>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
<script type="text/javascript">
var request;
function getRequestObject()
{
if (window.ActiveXObject)
{
return(new ActiveXObject("Microsoft.XMLHTTP"));
}
else if (window.XMLHttpRequest)
{
return(new XMLHttpRequest());
}
else {
return(null);
}
}
function sendRequest()
{
request = getRequestObject();
request.onreadystatechange = handleResponse;
var address = "patientList.jsp?searchBy=" + document.getElementById("searchBy").value + "&searchElement="+ document.getElementById("searchElement").value;
request.open("GET", address, true);
request.send(null);
}
function handleResponse()
{
if (request.readyState == 4 && request.status == 200)
{
document.getElementById("table").innerHTML = request.responseText;
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Patient</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<form id="f1" name="f1" method="post" onsubmit="ccheck();" >
<script>
$(document).ready(function() {
$("#datepicker").datepicker();
});
</script>
<section id="page" > <!-- Defining the #page section with the section tag -->
<header > <!-- Defining the header section of the page with the appropriate tag -->
<hgroup align="center">
<h3>Edit Patient</h3>
</hgroup>
</header>
<section id="articles"> <!-- A new section with the articles -->
<!-- Article 1 start -->
<div class="line"></div> <!-- Dividing line -->
<article id="article1"> <!-- The new article tag. The id is supplied so it can be scrolled into view. -->
<div class="articleBody clear">
search:
<select id="searchBy">
<option value="lastName">Last Name</option>
<option value="firstName">First Name</option>
<option value="name">Name</option>
<option value="city">City</option>
</select>
<input id="searchElement"/>
<input type="button" value="Search" onclick="sendRequest();"/>
</div>
</article>
<div id="table" align="center">
<jsp:include page="patientList.jsp" />
</div>
</article>
</section>
<footer> <!-- Marking the footer section -->
<div class="line"></div>
Go UP
</footer>
</section> <!-- Closing the #page section -->
<!-- JavaScript Includes -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="jquery.scrollTo-1.4.2/jquery.scrollTo-min.js"></script>
<script src="script.js"></script>
</form>
</body>
</html>
See if this links help you.
http://tympanus.net/codrops/2009/10/03/33-javascript-solutions-for-sorting-tables/
http://www.allmyscripts.com/Table_Sort/
Also let us know if you tried anything already
1.First store the dropdown /search value in Model class(using setter).
2.When you fired a query to fetch the details from database append the dropdown /search value which is stored in model class(using getter).
3.After fetch the value from DB render the dataTable .
Suggestion :
Please Follow the any one MVC architecture (Like Spring MVC architecture) to avoid the complexity of the your project.
Thanks you.
ASFAIK, The solution to your problem is ,you can use the jquery in jsp code, So you can find all Library's and include in it . There is no need to worry about sort and editing . Jquery has the Data Tables which has inbuilt API to sort the data in listed tables, its possible to edit the data in the table.
Reference Edit Reference Sort How to use data table in jsp pages
This is not exactly answers to question.
Try grid like jqGrid which takes care of things like sorting, searching, etc..

Categories