john
Servlet Response Send Redirect
2 Answer(s)      5 years and a month ago
Posted in : JSP-Servlet
Hi,

Thank you for your previous answer, the code works great. Sorry to bother you guys and perhaps this would be one of my last questions as I am almost finish with my web medical clinic app. Please if you guys can answer the following related to my last post regarding editing records.

1. In the code that you have given last time, I want to be able to do search on the Mysql table based on more than one column instead of one. what i mean is lets say user does a search on patient data based upon their id or last name. The result gets populated in the search table, user clicks a particular record but since this table has a patient who's visited many times, the id although FK is repeated and so the distinguishing element is the date of visit from the visit table. Can you please show me how I can pass more than one column value to the next jsp?

2. I have a servlet that saves patient data from a form into mysql table.I have some form validations through some javascript, but when the user clicks save or submit, there might be some Mysql errors that arise, I've catered to them by using JOptionPaneShowMessageDialogue in my servlet followed by a response.sendRedirect("index.jsp). the problem is that when a user has filled out a long form and gets a mysql error, I want instead of the response.sendRedirect is there some other method that the user doesn't loose the fields he already has filled because right now when I receive lets say a duplicate primary key(i have kept a non-auto increment), the user clicks ok on the message dialogue box and then index.jsp gets refreshed with all existing fields gone. Can you tell me a better way to do this?


Thank you So much,

Regards
Advertisement
View Answers

May 15, 2010 at 2:57 PM


Hi Friend,

Try the following code:

1)tablepopup.jsp:

<%@page import="java.sql.*"%>
<%
String id=request.getParameter("id");
String n=request.getParameter("name");
String address=request.getParameter("address");
String contact=request.getParameter("contactNo");
if((id!=null)&&(n!=null)&&(address!=null)&&(contact!=null)){
%>
<form action="edits.jsp">
<table border="1" >
<tr><td>Name:</td><td><input type="text" name="name" value="<%=n%>"></td></tr>
<tr><td>Address:</td><td><input type="text" name="address" value="<%=address%>"></td></tr>
<tr><td>Contact No:</td><td><input type="text" name="contactNo" value="<%=contact%>"></td></tr>
<tr><td></td><td><input type=submit name="edit" value="Edit"></td></tr>
</table>
<input type="hidden" name="id" value="<%=id%>">
</form>
<%
}
else{
%>
<form action="adds.jsp">
<table border="1">
<tr><td>Name:</td><td><input type="text" name="name" value=""></td></tr>
<tr><td>Address:</td><td><input type="text" name="address" value=""></td></tr>
<tr><td>Contact No:</td><td><input type="text" name="contactNo" value=""></td></tr>
<tr><td></td><td><input type=submit name="button" value="Add"></td></tr>
</table>
</form>
<%}
%>
<style>
.c1 {background-color: white;}
.c2 {background-color: white;}
.c3 {background-color: red;}
</style>
<script>
function ov(i){
document.getElementById(i).className="c3";
}
function ot(i,c){
document.getElementById(i).className=c;
}
function click(ide,d1,d2,d3){
var st=new String(d3);
if(st.length==1){
d3="0"+d3;
var d=d1+"-"+d2+"-"+d3;
window.location.replace('http://localhost:8080/examples/jsp/popup.jsp?id='+ide+'&&dd='+d,'mywindow','width=500, height=350,toolbar=no,resizable=yes,menubar=yes');
}
else{
var d=d1+"-"+d2+"-"+d3;
window.location.replace('http://localhost:8080/examples/jsp/popup.jsp?id='+ide+'&&dd='+d,'mywindow','width=500, height=350,toolbar=no,resizable=yes,menubar=yes');
}
}
</script>
</head>
<form>
Search:<input type="text" name="customer"><input type=submit value="Search">
</form>
<%
String name=request.getParameter("customer");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register","root","root";);
Statement st = con.createStatement();
ResultSet rs=st.executeQuery("select * from client where name='"+name+"'");
%>
<table id="table" border="1">
<%
while(rs.next()){
String d=rs.getString("visitedDate");
String sub1=d.substring(0,4);
String sub2=d.substring(5,7);
String sub3=d.substring(8,10);

System.out.println(sub1+" "+sub2+" "+sub3);
%>
<tr id=<%=rs.getString("id")%> class=c1 onclick='click(<%=rs.getString("id")%>,<%=sub1%>,<%=sub2%>,<%=sub3%>)' onmouseover='ov("<%=rs.getString("id")%>")' onmouseout='ot("<%=rs.getString("id")%>","c1")'><td><%=rs.getString("name")%></td><td><%=rs.getString("address")%></td><td><%=rs.getString("contactNo")%></td><td><%=rs.getString("visitedDate")%></td></tr>
<%
}%>
</table>
</html>


May 15, 2010 at 3:03 PM


continue..

2)popup.jsp:

<%@page import="java.sql.*"%>
<%
String id=request.getParameter("id");
String dd=request.getParameter("dd");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register","root","root";);
String query = "select * from client where id='"+id+"' and visitedDate='"+dd+"'";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);

String name="",add="",contact="",ide="";
while(rs.next()){
name=rs.getString("name");
add=rs.getString("address");
contact=rs.getString("contactNo");
ide=rs.getString("id");
}
response.sendRedirect("tablepopup.jsp?id="+ide+"&&name="+name+"&&contactNo="+contact+"&&address="+add);


%>

Please tell me whether index.jsp is the search page where we are displaying the search table.Also post the servlet code that saves the patient data.










Related Tutorials/Questions & Answers:
Servlet Response Send Redirect - JSP-Servlet
Servlet Response Send Redirect  Hi, Thank you for your previous... value to the next jsp? 2. I have a servlet that saves patient data from..., I've catered to them by using JOptionPaneShowMessageDialogue in my servlet
send redirect in JSP - JSP-Servlet
send redirect in JSP   How can I include a message i.e "redirected to this page because blah blah" in the send redirect page?  Hi friend, Please specify your problem the message print on the redirect to the next
Advertisements
Send Redirect in Servlet
Send Redirect in Servlet      ... servlet, then there we should use sendRedirect() method.  In send Redirect... or jsp. Then the servlet calls the sendRedirect() method of the response object
Send Redirect in Servlet
Send Redirect in Servlet     ... of our servlet, then there we should use sendRedirect() method.  In send... entered by the user is correct then the servlet will redirect the request
Send a Response Status in servlet
; This section illustrates you how to send response status in servlet. All... example shows how a response has been send from the servlet. If the requested... .style1 { font-weight: bold; } Send a Response
Redirect from servlet to servlet
Redirect from servlet to servlet  I want to insert data from Text box... to redirectServlet , and from this servlet I want to again forward my page to insertServlet(i.e...; <servlet-name>redirectServlet</servlet-name> <servlet
redirect to multiple links from servlet
redirect to multiple links from servlet  hello , In my servlet page... the output in browser,i need to redirect to another link like the above line. i want redirect to multiple links simultaneously. anyone help me out
servlet redirect problem help needed
servlet redirect problem help needed  package p; import...=getServletContext().getRequestDispatc‚?¶ re.forward(req, resp); } } this servlet is calling by a jsp..there is no problem,in this servlet the if condition always
servlet redirect problem help needed
servlet redirect problem help needed  package p; import...=getServletContext().getRequestDispatc‚?¶ re.forward(req, resp); } } this servlet is calling by a jsp..there is no problem,in this servlet the if condition always
Sendredirect In Servlet
Sendredirect In Servlet In this section you will read about the sendredirect() method. sendredirect() method is used to redirect the control to the different... this method is used to redirect the control to different server or domain a new request
send email with attachments in servlet
send email with attachments in servlet  How can we send an email with attachments in servlet?   Send attached email in Java Servlet
request header and response - JSP-Servlet
request header and response  hi sir, i have facing some problem in this qustion:- Create a servlet that accept a name and a phone number and contain 4 buttons add,update,delete and view .On clicking on the any of the button
Using redirect element & wild card
is generated by the new view using redirect response rather than rendering the response...Using redirect element & wild card          <redirect/>
Response Filter Servlet Example
Response Filter Servlet Example       This Example shows how to use of response filter in  Java Servlet. Filter reads own initial parameters and adds its value
web page altering while displaying servlet response
web page altering while displaying servlet response  in my application one index.jsp and one servlet is there and the servlet contains some db code and i want to display response of that servlet in a div in my index.jsp
Java Servlet : Http Response Headers
Java Servlet : Http Response Headers In this tutorial, you will learn how to Http Response Headers works in java servlet. Http Response Headers : Web server sends response with one or more response headers including the document
validating username and password in servlet and redirect to login page with error message if not valid
validating username and password in servlet and redirect to login page with error message if not valid  hi i want to validate my login page username and password in my servlet against database and if not valid want to display
send HTML Email with jsp and servlet
send HTML Email with jsp and servlet  Can You please show me how to send html Email using JSP and Servlet thank you
JSP:NEED RESPONSE ASAP PLEASE! - JSP-Servlet
JSP:NEED RESPONSE ASAP PLEASE!  Hi freind, If you could respond to my previous thread which hasn't been addressed for over a week, I'd really appreciate it. and most important if you could just respond to below. thank you I
calling servlet from JS and return response from servlet to JS
calling servlet from JS and return response from servlet to JS  hello...) which inturn calls a servlet(myServlet).servlet performs some DB related task.now i want this servlet(myServlet) to return a url(eg. pages/file.jsp
SMS Send and Recieve - JSP-Servlet
SMS Send and Recieve  How can i embedded SMS service to send and receive SMS from mobile to my application
How to send mail - JSP-Servlet
How to send mail  Thanks a lot ODBC is cleared to me now. Now,have another query about how we can send mail by using jsp. Actually i want to make a web page for a user feedback ,it must be send to our mail id
how to send a mail - JSP-Servlet
how to send a mail  Dear sir, I am able to send a mail.But when a receiver gets a mail ,the matter will be shown in a single line . I am sending a following matter Dear Harini , U r bonus is 2000
How to send data by ajax to servlet? - Ajax
How to send data by ajax to servlet?  Hi, Can i send a value from a HTML file to servlet via Ajax? If it is possible please give me a sample code Thanks
How to redirect from a HTML page?
/ValidUser"); Here is an example of Send Redirect in Servlet. Thanks...How to redirect from a HTML page?  Hi, Is it possible to redirect from HTML page to another page on the net? I have one page and I want to redirect
Exception: java.lang.IllegalStateException: getWriter() has already been called for this response - JSP-Servlet
Exception: java.lang.IllegalStateException: getWriter() has already been called for this response    response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); Statement st1
Send forgot Password through mail - JSP-Servlet
Send forgot Password through mail   hello every one I am... is provided) Now my question is how do i send the password if admin click on forgot password and i need to send that password through mail after answering a few
sendRedirect
; In send Redirect whenever the client makes any request it goes to the container, there the container decides whether the concerned servlet can handle the request or not.   If not then the servlet decides that the request can
Send Email From JSP & Servlet
J2EE Tutorial - Send Email From JSP & Servlet...' are collected  by the servlet and then processed for sending the mail...,HttpServletResponse response)     throws ServletException