passing data from jsp to servlet from html table - html

I am developing a web application using JSP and Servlets.
In that application I have to show data from database table stud(studID, name, add) in html table, And each row in the table will have a hyperlink associated with it at the last column. After clicking on that hyperlink I wants to get the (studID) from the table...
so far I have done getting the data from database and then putting it into the column and then adding hyperlink for each row.. But I am not able to get the (studID) from the html table associated with hyperlink..
Thanks in advance....
Source code :
<%
String[][] data = (String[][])request.getAttribute("data");
String[] cNames = (String[])request.getAttribute("columnNames");
//headings
%>
<table>
<tr>
<%
for(int i=0;i<cNames.length;i++) {
%>
<th>
<%= cNames[i] %>
</th>
<%
}
//data if(data!=null)
for(int i=0;i<data.length;i++) {
%>
<tr>
<%
for(int a=0;a<3;a++) {
%>
<td>
<%=
data[i][a]
%>
</td>
<%
//hyperlink
if(a==2) {
%>
<td>
<a href="PlanProtocol" id=<%=i%> onclick="<% session.setAttribute("ID","p2"); %>" >Edit</a></td>
<%
}
}
%>
</tr>
<% } %>
<tr>
</table>

You can pass the id as a query string in the url. Simply:
My Link
Will work. But if you are using JSTL or another tag library then you can do something like this:
<c:url value="/myservlet" var="myURL">
<c:param name="id" value="1234"/>
</c:url>
mylink
And this has its advantages such as url encoding etc.
So to add the id to the URL in your posted code you can:
<a href="PlanProtocol?id=<%=i%>" >Edit</a>
And the url will end up like this: PlanProtocol?id=1234.
In the Servlet you can get the Parameter by:
request.getParameter("i");
However, as I mentioned above, you probably want to use a tag library like the JSTL rather than placing these scriptlets in your page. There are several advantages.

think you should pull out the studID in JSP and format the studID into the query string of the URL, html page. (?studID=xxxxx) So the servlet will know the studID.

You can use request.setAttribute("studID","value"); in your jsp page to set the value and use request.getAttribute("studID"); in servlet to get value

Related

How to change button value attribute in JSP dynamically

I currently have a MySQL database with a table 'description' containing a 'title' and 'contents' variables. What I'd like to do, is dynamically create buttons who's value attributes are the 'title' of each 'description' row. Then I'd like to have the buttons display the 'contents' value when their respective 'title' is clicked.
The problem is I'm not sure how to go about inserting a JSP String variable into a button 'value' attribute dynamically. Is there any way of doing this without javascript?
This is the code I have:
Getting the description objects:
<%
List<Description> descriptions = DescriptionDB.getDescriptions();
%>
Scriptlet for the table of buttons:
<table border="1" id="titleTable">
<%
if (descriptions != null) {
for (Description description : descriptions) {
String title = description.getDescriptionTitle();
%>
<tr>
<td><a id="bt" type="button" value="title"</td>
</tr>
<%
}
}
%>
</table>
I would like the value="title" to be the String title in the scriptlet.
It should be as easy as
%>
<tr>
<td><a id="bt" type="button" value="<%= title %>"</td>
</tr>
<%
Be aware though that the use of scriptlets seems to be considered sort of bad practice by some today.

How to pass parameter to Java using HTML

I am new to Java and programming in general. For background, I am working on my simple survey system. I have all the classes/methods necessary to insert and pull from MySQL and display the survey on my JSP page.
This is my Index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Survey System</title>
</head>
<body>
<h1>Survey List</h1>
<%
ResultSet resultset = new controller.SelectSurvey().SelectSurvey();
if (request.getParameter("btnControlSurvey") != null){
response.sendRedirect("controlsurvey.jsp");
}
if (request.getParameter("btnTakeSurvey") != null){
response.sendRedirect("takesurvey.jsp");
}
%>
<form name = 'surveylist' action="index.jsp" method="POST">
<table border="0">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select id="listsurvey">
<% while (resultset.next()) {
%>
<option><%= resultset.getString("survey_ID")%> - <%= resultset.getString("title")%></option>
<% } %>
</select>
</td>
<td>
<input type="submit" value="TAKE SURVEY" name = "btnTakeSurvey" />
</td>
</tr>
</tbody>
<td>
<input type="submit" value="CONTROL SURVEY" name = "btnControlSurvey" />
</td>
</table>
</form>
</body>
How do I pass the selected surveyID value in my drop down list with ID listsurvey to my hard-coded surveyID1 variable in my Takesurvey.jsp (see below) once the Submit button is clicked?
<%
int surveyID1 = 1;
ResultSet rsSurvey = new controller.SelectSurvey().SelectSurveyByID(surveyID1);
ResultSet rsSurveyQuestion = new controller.SelectSurvey().SelectQuestionByID(surveyID1);
%>
I found that there are at least three ways to do this as listed below. What would be the easiest way and would you please give me an example?
Putting values into the session object.
Putting values into the application object
Putting values at the end of the redirect URL.
Your inputs are very appreciated.
Thank you and have a good day.
Not sure what you're doing in that 2nd code segment, but you can get a hold of the form parameters using
String surveyId = request.getParameter(listsurvey);
It looks like you're looking for an int on the other side, but it comes in as a String so just use Integer.parseInt() after you pull it from the request.
Note: The request variable is already implicitly available in your JSP. You don't have to declare it.
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter%28java.lang.String%29
You need to pass the surveyID in the HTTP page request using either GET or POST. Then in the servlet / JSP you can use request.getParameter("surveyID")
I figured it out by using request.getParameters and use different forms for each button and it works as wanted.
Thank you for all of yours input again guys.

Display query in jsp using html

How do I go about displaying the results of a query from MYSQL in a jsp using html tags? My instructor said to do the query in the .jsp file and use html tags to display the results. What is the correct syntax for this. Can I get an example or a link to a page that will show me. I have no problem writing the query, just unsure about the jsp/html display. Thanks
<BODY>
<H2>JSP Expressions</H2>
<UL>
<LI>Current time: <%= new java.util.Date() %>
<LI>Your hostname: <%= request.getRemoteHost() %>
<LI>Your session ID: <%= session.getId() %>
<LI>The <CODE>testParam</CODE> form parameter:
<%= request.getParameter("testParam") %>
</UL>
</BODY>
</HTML>
in jsp we have scriptlet tags ( <% %> ).you can use the jdbc statements between these jsp tags, The below sample code shows the display query in jsp using html,
<%#page import="java.sql.*,java.util.*,java.io.*"%>
<html>
<head>
<body background="1.jpg">
<title>ehealthcare advisor</title>
</head>
<body><center>
<h1><center>E-HEALTH CARE ADVISOR</center></h1>
<h4><center>get best solutions in minuites...</center></h4>
<table border="0" cellspacing="50" cell padding="10">
<tr>
<td> HOME</td>
<td> LOGOUT</td>
<td>ABOUT US</td>
</table>
</center>
<center>
<h2>PATIENT LIST</h2>
<table border="1" width="40%">
<thead>
<th>UserId</th>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th>Occupation</th>
<th>Height</th>
<th>Weight</th>
</thead>
<tbody>
<%
Statement st = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/ehealthcare","root","root");
st = con.createStatement();
String qry ="select * from user";
rs = st.executeQuery(qry);
while(rs.next()){ %>
<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>
<td><%=rs.getString(6)%></td>
<td><%=rs.getString(7)%></td>
</tr>
<%
}
con.close();
st.close();
}
catch(Exception ex){
out.println(ex);
}
%>
</tbody>
</table>
</body>
</html>
Here I give the link you refer it.
Jsp Sample Database Access, it is like core java
Jsp using JSTL tags
Database from Jsp
If you use No. 2 . Jsp using JSTL tags, that will be good programming.
JSTL tags are simple to learn.

Pass dynamic checkbox values to next JSP page

I have aproblem. Here I am showing some bills from Invoice table in mysql. And for single bill I am keeping a checkbox. I want to pass selected checkbox values to next jsp page. Please help me to do it. Thanks in advance.
<%
String company_name=request.getParameter("c_name");
try
{
Integer count=0;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_fleet","root","aadi");
Statement st = con.createStatement();
String sql="select invoice_no, invoice_date, gross_amount from tbl_invoice where client='"+company_name+"'";
ResultSet rs=st.executeQuery(sql);
%>
<center>
<table id="show_table">
<tr>
<td>PNR No</td>
<th>Date</td>
<th>Amount</td>
<th>Select</td>
</tr>
<%
while(rs.next())
{
%>
<tr>
<td><%=rs.getInt(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getInt(3)%></td>
<td><input type="checkbox" name="ck"/></td>
</tr>
%>
</table>
</center>
<%
}
catch(Exception e)
{
out.println(e);
}
%>
Hey I found the solution. On action page where I am going to receive values I will store all checkbox names in a array of string.
String selected_Checkboxes=request.getParameterValues("ck");
I will just change "td" tag of checkbox. I will put value of PNR NO which is coming from database and will put in checkbox.
<td><input type="checkbox" value="<%=rs.getInt(1)%>" name="ck"/></td>
I will use indexes of "selected_Checkboxes" to do further coding.
--Thanks Santino 'Sonny' Coleone

Ruby/Rails/HTML - Create New Table Row After X cells from loop

I'm using Rails to display a set of data. The problem is that data is so large I dont really do the usually for each loop since I creates this insanely long list.
My solution would be to create some form of table where after 10 records create a new cell and after 5 cells create a new row. I'm not really that comfortable with for loops in rails so I figured throw the question out.
Right now I have...
<strong> Person Data Set: </strong><br />
<% for person in #persons %>
<%= interest.name %> <br />
<% end %>
So I can I create a loop similar to this?
<strong> Person Data Set: </strong><br />
<table>
<tr>
*****for each 5 cells???? *****
<td>
*****For each 10 records?? ***
</td>
</tr>
</table>
Has anyone had to deal with an issue like this before?
There is an each_slice method. With HAML (I really don't like ERB but the idea is the same):
%strong
Person Data Set:
%br
%table
- #persons.each_slice(10) do |ten_people|
%tr
- ten_people.each_slice(5) do |five_people|
%td
- five_people.each do |person|
%p= person.name