am asking this question after some research. Am trying to open a text file located in my local maching with java and jsp. ie when I click a button in jsp it should open the text file for me. Could some one please help me on this.
Hi, Here is my code:
import java.awt.Desktop;
import java.io.File;
public class Start extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
try {
if ((new File("C:\\Debug\\log20.txt")).exists()) {
Process p = Runtime
.getRuntime()
.exec("C:\\Debug\\log20.txt");
p.waitFor();
} else {
System.out.println("File is not exists");
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Web.xml:
<servlet>
<servlet-name>LogFile</servlet-name>
<servlet-class>com.abc.def.LogFile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogFile</servlet-name>
<url-pattern>/logfile</url-pattern>
</servlet-mapping>
JSP:
<%# 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">
<% String status=""; %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>EMC eLicensing</title>
<link rel="stylesheet" type="text/css" href="css/css_ngoe/headerDefault.css" />
<link rel="stylesheet" type="text/css" href="css/css_ngoe/bodyTemplate.css" />
<link rel="stylesheet" type="text/css" href="css/css_ngoe/footer.css" />
<link rel="stylesheet" type="text/css" href="css/css_ngoe/helperClasses.css" />
<link rel="stylesheet" type="text/css" href="css/css_ngoe/railPanel.css" />
<link rel="stylesheet" type="text/css" href="css/css_ngoe/buttons.css" />
</head>
<body>
<!-- Header Start -->
<div class="parentheader">
<div id="header">
<h1>
E
</h1>
<div id="header-text-position">
<div id="header-text"></div>
<br />
</div>
</div>
</div>
<!-- Header End -->
<br>
<br>
<table width=100% cellpadding=0 cellspacing=0 border=0>
<tr>
<td> </td>
<!-- START CONTENT -->
<td>
<P>
lmgrd options
<BR>
<BR>
<FORM action="start" METHOD="GET">
<table
cellspacing="5" cellpadding="1" border="0">
<tr>
<td>Start/Stop/Reread</td>
<td><input type=submit name=txtSubmit1 id=txtSubmit value="Start" /></td>
</tr>
</table>
</FORM>
<FORM action="stop" METHOD="GET">
<table
cellspacing="5" cellpadding="1" border="0">
<tr>
<td>Start/Stop/Reread</td>
<td><input type=submit name=txtSubmit2 id=txtSubmit value="Stop" /></td>
</tr>
</table>
</FORM>
<FORM action="logfile" METHOD="GET">
<table
cellspacing="5" cellpadding="1" border="0">
<tr>
<td>LOGFILE</td>
<td><input type=submit name=txtSubmit3 id=txtSubmit value=LOG /></td>
</tr>
</table>
</FORM>
<form action="start" method="GET"
enctype="multipart/form-data">
<input type="file" name="file"
value=text />
<input type="submit" />
</form>
<form action="status" method=GET>
<BR>
<BR>
<input type=submit name=Submit id=txtSubmit
value=Status>
</form>
<BR>
status: <%=status %>
<p>
<p>
<p>
</td>
</tr>
</table>
</body>
</html>
You Could you try it like this if you want to browse the file in an external program
Process p = Runtime.getRuntime().exec("C:\\Path\\to\\notepad.exe C:\\Debug\\log20.txt");
And if you want to browse the file in web page you could try the following:
PrintWriter out = res.getWriter();
File file = new File("C:\\Debug\\log20.txt");
if (file.exists()) {
BufferedReader input = new BufferedReader(new FileReader(file));
String line = "";
while ((line = input.readLine()) != null) {
out.println(line);
}
}
Related
I have an application where in a JSP page i am displaying a drop down list but i am getting an exception in my code.
public class ExpenseCreationBean {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
Controller Class:-
#RequestMapping(value = "/addDetails", method = RequestMethod.GET)
public String getExpenseEntryPage(Model model) {
ExpenseCreationBean expenseCreationBean = new ExpenseCreationBean();
model.addAttribute("expenseCreationBean", expenseCreationBean);
List<String> coloursList = new ArrayList<String>();
coloursList.add("red");
coloursList.add("green");
coloursList.add("yellow");
coloursList.add("pink");
coloursList.add("blue");
model.addAttribute("colours", coloursList);
System.out.println("I was here!!");
return "addDetails";
}
addDetails.jsp Page
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<html>
<head>
<title>Add Details</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(function() {
$("#datepicker").datepicker({
showOn : "button",
buttonImage : "images/calendar.png",
buttonImageOnly : true,
buttonText : "Select date"
});
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>Expense Entry Details</h1>
<form:form method="post" action="savedata" modelAttribute="expenseCreationBean">
<table border="6px" cellspacing="10px" cellpadding="10px">
<tr>
<td>Date Of Purchase: <input type="text" id="datepicker"
name="date_of_purchase"></td>
<td>Item Name:<input type="text" name="description"></td>
<td>Please select:</td>
<td><form:select path="color">
<form:option value="" label="...." />
<form:options items="${colours}" />
</form:select>
</td>
<td>Paid By: <select name="paid_by"></td>
<td>Amount Paid:<input type="text" name="total_price"
id="total_price"></td>
<td>Quantity:<input type="text" name="quantity_purchased"></td>
<td>Unit:<input type="text" name="unit"></td>
</tr>
<tr>
<tr>
<tr>
<tr>
<td>Exclude:</td>
<td><input TYPE="checkbox" name="exclude">
</tr>
<tr>
<td>Comments:<textarea rows="3" cols="25" name="comments"></textarea>
</td>
</tr>
<tr>
<td><input type="submit" value="Save" align="middle"></td>
</table>
</form:form>
</body>
</html>
I am getting the below exception :-
javax.servlet.jsp.JspException: Type [java.lang.String] is not valid for option items
org.springframework.web.servlet.tags.form.OptionWriter.writeOptions(OptionWriter.java:143)
org.springframework.web.servlet.tags.form.OptionsTag.writeTagContent(OptionsTag.java:157)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
It is just a Spring MVC Web application where i am trying to display the drow down list pre-populated with the colors data.
Any help is highly appreciated.
I added the below line on teh top of addDetails.jsp file and it worked:-
Try to add into Map, instead of ArrayList.
Map<String,String> coloursList = new HashMap<String,String>();
coloursList.put("R","red");
coloursList.put("R","green");
coloursList.put("Y","yellow");
coloursList.put("P","pink");
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..
I wrote a jsp project, tried to execute it. I got this error:
listType cannot be resolved to a variable
So I added this lines to my jsp file:
<%#page import="java.util.List"%>
<%#page import="java.util.ArrayList"%>
But it didn't fix the problem. How can I fix it?
There is one more error like this:
The method StudentDetails() is undefined for the type
MangeUser Student.jsp
Here is Student.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="com.junos.users.MangeUser"%>
<%-- <%# page import="com.junos.userdetails.UserEntityDetails"%> --%>
<%#include file="menu.jsp" %>
<%#page language="java" import="java.util.*,java.io.*,java.sql.*"%>
<%!int skillLevel_id=0;%>
<jsp:useBean id="Student" class="com.junos.users.MangeUser" />
<jsp:setProperty name="Student" property="*" />
<!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=ISO-8859-1">
<title> Users </title>
<link href="<c:url value="/resources/data-tables/css/jquery.dataTables.css" />"
rel="stylesheet" type="text/css" />
<script src="<c:url value="/resources/data-tables/js/jquery.dataTables.js" />"type="text/javascript"></script>
<link href="<c:url value="/resources/bootstrap-select/css/bootstrap-select.min.css" />"
rel="stylesheet" type="text/css" />
<script src="<c:url value="/resources/bootstrap-select/js/bootstrap-select.min.js" />"></script>
<style type="text/css">
.bootstrap-select:not([class*="col-"]):not([class*="form-control"]):not(.input-group-btn) {
width: 218px;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
var table = $('#usrTable').DataTable();
$('#UserDiv').hide();
$('#tblsDiv').hide();
$('#challenegrDescriptionDiv , #Image ').hide();
$('#addUser').click(function(){
$('#UserDiv').show();
$('#usrTbl').hide();
});
$('.cncl').click(function(){
$('#usrTbl').show();
$('#UserDiv').hide();
});
});
var roleValue = function(){
var val = $('#userRole_id option:selected').text();
if(val == "WAITER"){
$('#tblsDiv').show();
}else{
$('#tblsDiv').hide();
}
if(val == "CHALLENGER"){
$('#usrnmDiv , #passDiv , #cnfrmPassDiv').hide();
}else{
$('#usrnmDiv , #passDiv , #cnfrmPassDiv').show();
}
if(val == "CHALLENGER"){
$(' #Image , #description').show();
$('#challenegrDescriptionDiv , #Image ').show();
}else{
$(' #Image , #description').hide();
$('#challenegrDescriptionDiv , #Image ').hide();
}
}
</script>
<script type="text/javascript">
function PreviewImage() {
alert("PreviewImage");
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("profileImage").files[0]);
oFReader.onload = function (oFREvent) {
document.getElementById("imagePreview").src = oFREvent.target.result;
};
};
</script>
<script type="text/javascript">
//getting value of radio button
$(document).ready(function(){
$("input[type='radio']").click(function(){
var radioValue = $("input[name='name']:checked").val();
if(radioValue){
alert("Your are a - " + radioValue);
}
window.location='EditList.jsp?radioValue='+radioValue;
});
});
</script>
</head>
<body >
<div class="container">
<div class="row-fluid">
<div id="UserDiv" class="col-md-8 col-md-offset-2">
<div class="well well-sm">
</div>
</div>
</div>
</div>
<div id="usrTbl" class="col-md-10 col-md-offset-1">
<div class="btn-group" style= "margin-bottom: 10px;">
<button id="addUser" class="btn btn-success" style="margin-left: 10px;">
<span class="glyphicon glyphicon-plus-sign"></span>
</button>
<button class="btn" style="background-color: #9933CC; color: white;margin-left: 10px;">
<span class="glyphicon glyphicon-edit"></span>
</button>
<button class="btn btn-danger" style="margin-left: 10px;">
<span class="glyphicon glyphicon-trash"></span>
</button>
</div>
<table id="usrTable" class="display text-center table-bordered table-striped" cellspacing="0" width="100%">
<thead>
<tr>
<th class="text-center">First Name</th>
<th class="text-center">Last Name</th>
<th class="text-center">User Name</th>
<th class="text-center">Password</th>
<th class="text-center">Mobile Number</th>
<th class="text-center">Email Id</th>
<th class="text-center">Challenger Description</th>
</tr>
</thead>
<tbody>
<%
List<MangeUser> userList= Student.StudentDetails();
System.out.println("userList size: " + userList.size());
pageContext.setAttribute("Users", userList);
%>
<c:forEach items="${Users}" var="user">
<tr>
<td><c:out value="${user.firstName}"/></td>
<td><c:out value="${user.lastName}"/></td>
<td><c:out value="${user.username}"/></td>
<td><c:out value="${user.password}"/></td>
<td><c:out value="${user.mobileNumber}"/></td>
<td><c:out value="${user.emailID}"/></td>
<td><c:out value="${user.challengerDescription}"/></td>
</tr>
</c:forEach>
</tbody>
</table>
<%#include file="footer.jsp" %>
</div>
</body>
</html>
How can I fix those 2 errors?
I've been trying to get this code to work. THe basic idea is I want to create an ajax request when the textbox whose id is "user" to go back to the server (apache) via an ajax call.
The error I get is a 404 when doing a http://localhost:8080/CheckUserAvailabilityAction.do?username=ds request.
This is my code.
Struts2 class CheckUserAvailabilityAction.java:
public class CheckUserAvailabilityAction extends ActionSupport implements
ServletRequestAware {
//struts action
public String execute() {
String output = "success";
/*
SOME CODE
*/
return output;
}
}
Here's my jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="js/scripts.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
<script>
var url = "HelloWorld.jsp";
function makeRequest(url) {
if (window.XMLHttpRequest) {
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { }
}
}
if (!httpRequest) {
return false;
}
httpRequest.onreadystatechange = alertContents();
httpRequest.open('GET', url);
httpRequest.send();
//Redirecting to the HelloUser.jsp
window.location.replace(url);
}
function alertContents() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
alert(httpRequest.responseText);
} else {
alert('There was a problem with the request.');
}
}
}
$(function(){
$("#username").blur("/CheckUserAvailability");
});
function checkUserAvailability(){
var name = $("#username").val();
$.get(
"/CheckUserAvailabilityAction.do",
{ 'username': name },
function () {
var response = $(data).find("response").text();
$("#response").hide();
$("#response").empty();
$("#response").append(response);
$("#response").show("slow");
}
);
}
</script>
<link rel="stylesheet" type="text/css" href="css/styles.css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>New user - Registration form</title>
</head>
<body id="bodyStyling">
<%
if ("noSexSelected".equalsIgnoreCase(request
.getParameter("register"))) {
%>
<div id="registrationFailure">
<h4>Please select a Sex</h4>
</div>
<%
}
%>
<form name="myForm" action="checkUserNameAvailability" method="post">
<!-- onsubmit="return validateForm(this);" -->
<table>
<tr>
<td><h2 id="IntroductionReg">New User Registration</h2></td>
</tr>
<tr>
<td class="myTableDataStyling">Name:</td>
<td><input name="username" id="username" type="text"
onblur="checkUserAvailability()" /></td>
<td><label id="usrLabel"></label></td>
</tr>
<tr>
<td class="myTableDataStyling">Password:</td>
<td><input name="password" type="password" onclick= /></td>
</tr>
<tr>
<td class="myTableDataStyling">Confirm password:</td>
<td><input name="passwordConfirmation" type="password"
onclick="this.value''" /></td>
</tr>
<tr>
<td class="myTableDataStyling">Age:</td>
<td><input name="age" onclick="this.value=''" /></td>
</tr>
<tr>
<td class="myTableDataStyling">Sex:</td>
<td><select name="sex">
<option value="">- Select -</option>
<option value="M">M</option>
<option value="F">F</option>
</select>
</tr>
<tr>
<td class="myTableDataStyling">Address:</td>
<td><input name="address" onclick="this.value=''" /></td>
</tr>
<tr>
<td class="myTableDataStyling">E-mail:</td>
<td><input name="email" onclick="this.value=''" /></td>
</tr>
<tr>
</tr>
</table>
<input type="submit" value="Register user" />
</form>
<div id="outputDiv"></div>
<div id="response"></div>
</body>
</html>
This is my struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Here, I declare the actions for the com.tcs.controller package classes -->
<package name="damo" extends="struts-default">
<action name="process" class="com.tcs.controller.User">
<result name="success">/process.jsp</result>
</action>
<action name="login" class="com.tcs.action.LoginAction">
<result name="success">HelloUser.jsp</result>
<result name="error">Login.jsp</result>
</action>
<action name="register" class="com.tcs.action.RegisterUserAction">
<result name="success">process.jsp</result>
<result name="error">process.jsp</result>
</action>
<action name="checkUserNameAvailability" class="com.tcs.action.CheckUserAvailabilityAction">
<result name="success" type='redirect'>register.action</result>
<result name="error">process.jsp</result>
</action>
</package>
</struts>
I'm struggling to get my checkUserAvailability function to make a valid request, I'm lost since I have minimal knowledge of struts.
Help is greatly appreciated
Thanks.
Please refer the below url for exact solution.
Send json data to struts 2 ajax
Dynamic Drop Down List
I think you're missing your project name from the url. Could you test
http://localhost:8080/[yourProjectName]/CheckUserAvailabilityAction.do?username=ds
so that if your project in your workspace is called foo you'd call url
http://localhost:8080/foo/CheckUserAvailabilityAction.do?username=ds
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..