Submitting form with multiple dropdown boxes? - java

in the below code i am having multple dropdown boxes inside a single form.In dropdown boxes i am showing the default values.
but when a user gets sign in then the user can change the values inside dropdowns and clicks submit button.
<form method="post" id="myForm" name="myForm">
<%
while(rs1.next())
{
%>
<table id="main_table" style="width: 600px; border: 0px solid #ccc; padding: 1px; border-spacing: 1px; ">
<tbody>
<tr class="ab" style="background-color:#3f92aa; color:white; width:600px; ">
<td colspan="2" class="flip" style="width:125px; font-size:14px; text-align:left; font-family:Palatino Linotype;"><%=rs1.getString(3) %><input type="hidden" name="statename" value="Goa"></td>
<td colspan="2" class="flip" style="width:125px; font-size:14px; text-align:center; font-family:Palatino Linotype;"><%=rs1.getString(4) %></td>
<td colspan="2" class="flip" style="background-color:#cb3315; width:125px; font-size:14px; text-align:center; font-family:Palatino Linotype;"><%=rs1.getString(5) %></td>
<td colspan="2" style="width:125px; font-size:13px; font-family:Palatino Linotype; text-align:center;">
<table style="border: 0px;border-spacing: 0px;padding: 0px;">
<tr>
<td> </td>
<td>
<select name="<%=rs1.getString(3) %>">
<option value="<%=rs1.getString(5) %>"><%=rs1.getString(5) %></option>
<%
Statement stmt2=null;
DBconnection db2=new DBconnection();
Connection con2=db2.dbConn();
try{
stmt2 = con2.createStatement();
ResultSet rs2 = (ResultSet) stmt2.executeQuery("select distinct WinnerParty from election_history;");
while(rs2.next())
{
%>
<option value="<%=rs2.getString(1) %>"><%=rs2.getString(1) %></option>
<%
}
}
catch(SQLException e){
e.printStackTrace();
}
finally{
con2.close();
stmt2.close();
}
%>
</select>
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</tbody>
</table>
</form>
Servlet code:
con=DBconnection.dbConn();
stmt=con.createStatement();
String statenames=request.getParameter("statename");
System.out.println("this is State name ==="+statenames);
ResultSet rs = (ResultSet) stmt.executeQuery("select distinct Constituency from election_history where State='"+statenames+"'");
//rs.next();
while(rs.next()){
constituencies.add(rs.getString(1));
}
System.out.println("list of constituencies are=="+constituencies);
for(int i=0;i<constituencies.size();i++)
{
String Cname=constituencies.get(i);
System.out.println("Cname value is=="+Cname);
String Cselected=request.getParameter(Cname);
System.out.println("Cselected value is=="+Cselected);
map.put(Cname, Cselected);
}
Output:
inside editYPName...
Connected to the database
this is State name ===Goa
list of constituencies are==[Panaji, Mormugao]
Cname value is==Panaji
Cselected value is==null
Cname value is==Mormugao
Cselected value is==INC
But here whats my problem is when i am submiting a form i am getting only one value(CSelected) of dropdown inside servlet as you can see inside output and remaining values as null.
can anyone tell me what is the issue here??

If you have multiple drop downs all named statename, don't use
String statenames = request.getParameter("statename"); but rather use:
String[] statenames = request.getParameterValues("statename");

Related

Session servlet does not work on drop down menu jsp

This is my coding for order.jsp, a form for customer to insert order and I use session.menus to display option menu in a HTML input element from database. I already connect to database using jdbcutility.java, where I put sql statement to connect my servlet with database and its working for login but not for this session.
<form class="form-horizontal" method="post" action="InsertOrderServlet">
<center>
<table border="10" cellpadding="20" cellspacing="20">
<thead>
<tr>
<th colspan="2"><h2 style="line-height: 3;" >Order Food</h2></th>
</tr>
</thead>
<tbody>
<tr>
<td><br><br>Food</br></br></td>
<td>
<br><br>
<div class="col-lg-9">
<select class="form-control" name="menuName">
<c:forEach items="${sessionScope.menus}" var="currentmenu" varStatus="loop">
<option><c:out value="${currentmenu.foodName}" /></option>
</c:forEach>
</select>
</div>
</td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="quantity" required/></td>
</tr>
<tr style="line-height: 10;" >
<td> <button class="btn btn-primary" type="reset" >Cancel</button></td>
<td > <button type="submit" class="btn bt primary">Submit</button></td>
</td>
</tr>
</tbody>
</table>
</center>
</form>
getMenuServlet.java
//select all from menu
try {
ResultSet rs = jdbcUtility.getPSSelectMenu().executeQuery();
while (rs.next()) {
menu = new Menu();
menu.setId(rs.getInt("menuid"));
menu.setfoodName(rs.getString("foodName"));
menu.setImage(rs.getString("image"));
menu.setPrice(rs.getDouble("price"));
//put into arraylist
menus.add(menu);
}
}
catch (SQLException ex)
{
}
//put into sessions
session.setAttribute("menus", menus);
jdbcutility.java
String sqlValidateLogin = "Select username,password from customer where username=? and password=?";
psInsertLogin = con.prepareStatement(sqlValidateLogin);
String sqlInsertOrder = "INSERT INTO orderfood(menuName, quantity) " + "VALUES(?, ?)";
psInsertOrder = con.prepareStatement(sqlInsertOrder);
String sqlSelectAllMenu ="SELECT * FROM menu";
psSelectAllMenu = con.prepareStatement(sqlSelectAllMenu);
String sqlSelectAllOrder ="SELECT * FROM orderfood";
psSelectAllOrder = con.prepareStatement(sqlSelectAllOrder);
This keep occurring, how do I fix this?
image blank
Make sure the JSP is allow access session.
<%# page session="true" %>
To use core JSTL, make sure the following code is included.
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

How to return XML data from a JSP?

From a jsp page I have to return xml data.
I fetch a record from the database and display in the Jsp. I have to return a display record in xml. And I have to do both display in jsp and same time return a record in xml.
I already did a display process and struck to return in xml:
<%
String selectsql = null;
DBConnection DBCon = null;
Connection Con = null;
String maprule = null;
PreparedStatement pstm = null;
ResultSet resultSet = null;
String snomedid = request.getParameter("snomedcode");
String snomedname = request.getParameter("snomednames");
try {
DBCon = new DBConnection();
Con = DBCon.getConnection();
selectsql = "SELECT referencedComponentId,sctName,mapTarget,icdName,mapRule,refid FROM snomedicd10map WHERE referencedComponentId=? or sctName=? ";
pstm = Con.prepareStatement(selectsql);
pstm.setString(1, snomedid);
pstm.setString(2, snomedname);
resultSet = pstm.executeQuery();
%>
<h2 align="center">
<font><strong>SNOMED CT TO ICD 10 </strong></font>
</h2>
<div style='min-height: 40px'>
<table id="tblMain" width="100%" class='table1' border=0
cellpadding='5' cellspacing='0' style='topmargin: -5px'>
<tr>
<td colspan='4' style='text-align: right;'>
<!-- <input type="button" value="Back" onclick="goback();" style="background-color: #3399ff;color:#ffffff" /> -->
<button onclick="goBack()" style="background-color: #3399ff;color:#ffffff">Back</button>
</td>
</tr>
<tr class='headerText' style='height: 25px'>
<td class='tdcellblue' style='width: 10%'>SNOMED</td>
<td class='tdcellblue' style='width: 40%'>SNOMED Name</td>
<td class='tdcellblue' style='width: 10%'>ICD-10</td>
<td class='tdcellblue' style='width: 40%'>ICD Name</td>
</tr>
<%
while (resultSet.next()) {
//String maprule = null;
maprule = (resultSet.getString("mapRule"));
if (maprule.matches("OTHERWISE TRUE")
|| maprule.matches("TRUE")) {
%>
<tr>
<td><%=resultSet.getString("referencedComponentId")%></td>
<td><%=resultSet.getString("sctName")%></td>
<td><%=resultSet.getString("mapTarget")%></td>
<td><%=resultSet.getString("icdName")%></td>
</tr>
<%
}
}
%>
</table>
</div>
<%
if(request.getParameter("hiderefineproblem")==null && maprule.matches("OTHERWISE TRUE")){ %>
<input type="button" value="Refine Problem" onclick="return showHide();" style="background-color: #3399ff;color:#ffffff;" />
<%}%>
<div id="showHideDiv" style="display: none;">
<p>Would one of the following diagnoses apply? Choose the most
specific one:</p>
<FORM ACTION="snomedMapping.jsp#newres" METHOD="POST">
<%
pstm = Con.prepareStatement(selectsql);
pstm.setString(1, snomedid);
pstm.setString(2, snomedname);
resultSet = pstm.executeQuery();
boolean bSubmit=false;
int refid=0;
String[] pipe=null;
while (resultSet.next()) {
refid=resultSet.getInt("refid");
pipe= resultSet.getString("mapRule").split("\\|");
if (pipe.length > 1){
bSubmit=true;
%>
<input type="radio" id="radioList" value="<%=refid%>" name="refId"/>
<tr><%=pipe[1]%></tr>
<br />
<%
}
}
%>
<%if(bSubmit){%>
<input type="hidden" name='hiderefineproblem' value='yes'/>
<INPUT TYPE="SUBMIT" value="Submit" style="background-color: #3399ff;color:#ffffff;">
<%}%>
</FORM>
</div>
<script>
function showHide() {
var ele = document.getElementById("showHideDiv")
if (ele.style.display == "block") {
ele.style.display = "none";
} else {
ele.style.display = "block";
}
}
/* function goback(){
window.location.href='/mobiledoc/jsp/catalog/xml/migration/snomedMapping/snomedIndex.jsp';
} */
function goBack() {
window.history.back();
}
</script>
<h2 align="center">
<font><strong></strong></font>
</h2>
<div style='min-height: 40px' id="newres">
<!-- <table id="tblMain" width="100%" class='table1' border=0
cellpadding='5' cellspacing='0' style='topmargin: -5px'>
<tr class='headerText' style='height: 25px'>
<td class='tdcellblue' style='width: 10%'>SNOMED</td>
<td class='tdcellblue' style='width: 40%'>SNOMED Name</td>
<td class='tdcellblue' style='width: 10%'>ICD-10</td>
<td class='tdcellblue' style='width: 40%'>ICD Name</td>
</tr> -->
<table>
<%
String str=request.getParameter("refId");
if(str.length()>0){
refid=Integer.parseInt(str);
//resultSet=Root.EcwCloseResultSet(resultSet);
//pstm=Root.EcwClosePreparedStatement(pstm);
selectsql = "SELECT * FROM snomedicd10map WHERE refid=?";
pstm = pstm = Con.prepareStatement(selectsql);
pstm.setInt(1,refid);
resultSet = pstm.executeQuery();
while (resultSet.next()) {
%>
<tr>
<td colspan='2' style='font-weight:bold;'>SNOMED</td>
</tr>
<tr>
<td><%=resultSet.getString("referencedComponentId")%> </td>
<td><%=resultSet.getString("sctName")%></td>
</tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr></tr>
<tr>
<td colspan='2' style='font-weight:bold;'>Target ICD</td>
</tr>
<tr>
<td><%=resultSet.getString("mapTarget")%> </td>
<td><%=resultSet.getString("icdName")%></td>
</tr>
<%
}
}
%>
</table>
</div>
<% }
How to implement it? I need to create one more page or in same page.
Instead of returning HTML text from the JSP, you just return XML text.
The code that is provided has HTML tags and JSP code to create the HTML document structure and content.
The XML version of the JSP will have XML tags and JSP code to create the XML document structure and content.

How to write a function in jsp which can be called when the submit button is clicked

I have the following code:
<tr>
<td>Text:</td>
<td><input type="text" name="myText" id="myText" value=""></td>
<td><input type="button" value="Click to Submit"></td>
</tr>
<%
String selectSQL = "select * from INTERFACE where INTERFACE_SHORT_NAME='?'";
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection(
"jdbc:xyz:thin:#abc:1521:pqr", "user", "pw");
PreparedStatement statement = connection.prepareStatement(selectSQL) ;
statement.setString(1,request.getParameter("myText"));
ResultSet resultset = statement.executeQuery();
%>
<TABLE BORDER="1">
<TR>
<TH>INTERFACE_SHORT_NAME</TH>
</TR>
<% while(resultset.next()){ %>
<TR>
<TD><%= resultset.getString("INTERFACE_SHORT_NAME") %></td>
</TR>
<% } %>
</TABLE>
<% }
catch(SQLException e){
System.out.println("Class not found ");
}
%>
</body>
</html>
I want the query to fetch data on the submit button click but I am finding it difficult to figure out the use of onClick.
Also, the "setString" which i have written does not execute. I am not sure where I am going wrong.
Change your button as submit button -
<input type="submit" value="Click to Submit">
and make sure your put this button inside a form.

how to get value using model and scriptlets

I am creating a table in JSP in which i am displaying some values using hard code.
i want to display those value using scriptlets and model. Someone help me how to do that?
<tr>
<td>Automobile</td>
<td>11-JUN-2015</td>
<td>10-FEB-2016</td>
<td>Cars and bikes</td>
</tr>
Getting values from the database:
<%rst=stmt.executeQuery("select * from auto_details");%>
Sample Html Table:
<html>
<body>
<center>
<h2>AutoList</h2>
<table border="1" cellspacing="0" cellpadding ="0">
<tr>
<td><b>S.No</b></td>
<td><b>Date</b></td>
<td><b>typeofvichele</.b></td>
</tr>
<%
int no=1;
while(rst.next()){
%>
<tr>
<td><%=no%></td>
<td><%=rst.getString("date")%></td>
<td> <%=rst.getString("typeofvichele")%> </td>
</tr>
<%
no++;
}
rst.close();
stmt.close();
con.close();
%>
</table>
</center>
</body>
</html>

JSP how to store image into database from html input

I'm trying to write a code that stores images into database, then later display it on the webpage using the rollnum to retrieve it from the database. I've tried all i could but still no good, i tried reading some tutorials but still no good, anyways here's the code i'm working on. I'd be very grateful if someone could help, Thanks in Advance.
<%#page import="java.sql.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration Page</title>
<style>
body{
background-image: url('bg.png');
background-repeat: repeat;
font-family: Courier New;
margin:auto;
width: 1000px;
}
.wrap{
margin-top:10px;
border:solid 1px #000;
height:900px;
}
.header{
font-size: 50px;
font-weight: bold;
font-family: Courier New;
text-align: center;
}
.border{
border-bottom: solid 1px #000;
margin-left: 30px;
margin-right: 30px;
}
.box{
border: solid 1px #000;
padding: 8px;
margin: 30px;
}
</style>
</head>
<body>
<div class="wrap">
<table>
<form method="post">
<br/>
<tr>
<td> Roll Number:</td> <td><input type="text" name="rollnum"/></td>
</tr>
</div>
<tr>
<td> First Name:</td> <td><input type="text" name="fname"/></td>
</tr>
<tr>
<td> Last Name:</td> <td><input type="text" name="lname"/></td>
</tr>
<tr>
<td> Course Name:</td> <td><input type="text" name="course"/></td>
</tr>
<tr>
<td> Gender:</td> <td>Male:<input type="radio" name="gender" value="Male"/> Female:<input type="radio" name="gender" value="Female"/></td>
</tr>
<tr>
<td> Address:</td> <td><input type="text" name="addr"/></td>
</tr>
<tr>
<td> State:</td> <td><input type="text" name="state"/></td>
</tr>
<tr>
<td> Photo:<input type="file" name="photo"/></td>
</tr>
<tr>
<td> <input type="submit" value="Submit" name="submit"/></td>
</tr>
</form>
</table>
<%
try{
if(request.getParameter("submit") != null){
Connection conn;
PreparedStatement prep;
String sql;
String url = "jdbc:derby://localhost:1527/reg";
Class.forName("org.apache.derby.jdbc.ClientDriver");
conn = DriverManager.getConnection(url,"uname","pass");
sql =
"insert into student (rollnum,fname,lname,course,gender,addr,state,photo)"
+"values(?,?,?,?,?,?,?,?)";
ResultSet rs = null;
prep = conn.prepareStatement(sql);
prep.setInt(1, Integer.parseInt(request.getParameter("rollnum")));
prep.setString(2, request.getParameter("fname"));
prep.setString(3, request.getParameter("lname"));
prep.setString(4, request.getParameter("course"));
prep.setString(5, request.getParameter("gender"));
prep.setString(6, request.getParameter("addr"));
prep.setString(7, request.getParameter("state"));
//prep.setString(8, request.getParameter("photo"));
Blob blob = rs.getBlob(request.getParameter("photo"));;
prep.setBlob(8, blob);
int n = prep.executeUpdate();
if(n>0){
%>
<div class="box">Registration Successful!! <b>Login Now!!</b></div>
<%
}
}
}
catch(ClassNotFoundException e){
}
catch(NumberFormatException n){
}
%>
</div>
</body>
You can follow this way:
Get the image
Convert the image with the base64 rappresentation
Store the base64 into your db
when you need the image convert again the base64 into the image

Categories