Session servlet does not work on drop down menu jsp - java

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"%>

Related

html code tables is not workinig

i cannot show any values and i dont get any error messages. . here is my code:
Number: <input type="text" name="number" />
<br>
<% Persone =(Employqq)request.getAttribute("cust"); %>
<table id="table" border="1">
<tbody>
<tr>
<th>Number</th>
</tr>
for(Employee t : emp){ %>
<tr>
<td>
<%= t.getnumber() %>
</td>
<% } } %>
</tbody>
</table>
i appreciate all the help that i can get
You set the attribute in your servlet as Cust, and in your JSP you read it as cust

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.

No output info on .JSP from MySQL DB, Spring MVC, Hibernate, JAVA

I'm trying to output some information from table users on my JSP page, but there is no output there.
Here is my controller method:
#Autowired
private UsersService usersService;
#RequestMapping(value="/user**")
public String getUserProfile( ) {
ModelAndView mav = new ModelAndView("userInfo");
List<Users> userInfo = usersService.userInfo("Oleg");
mav.addObject("userInfo", userInfo);
return "user";
}
Here is my DAO method
#SuppressWarnings("unchecked")
#Override
public List<Users> userInfo(String username) {
Transaction tx = sessionFactory.getCurrentSession().beginTransaction();
Session session = this.sessionFactory.getCurrentSession();
String query = "select users.username, users.name, users.surname, users.email, users.gender, users.age, users.weight, users.height, users.sport, users.place from users where users.username LIKE '%s'";
List<Users> userInfo = session.createSQLQuery(String.format(query,username)).list();
tx.commit();
return userInfo;
}
Here is my Service layer method
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>User Profile Page</title>
</head>
<body>
<br />
<br />
<br />
<h1>User profile page !!!</h1>
<c:url var="logoutUrl" value="j_spring_security_logout" />
<form action="${logoutUrl}" method="post">
<input type="submit" value="Log out" /> <input type="hidden"
name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
<c:forEach var="users" items="${userInfo}">
<table>
<tr>
<td>Username</td>
<td>${users.username}</td>
</tr>
<tr>
<td>First Name</td>
<td>${users.name}</td>
</tr>
<tr>
<td>Last Name</td>
<td>${users.surname}</td>
</tr>
<tr>
<td>Email</td>
<td>${users.email}</td>
</tr>
<tr>
<td>Gender</td>
<td>${users.gender}</td>
</tr>
<tr>
<td>Age</td>
<td>${users.gender}</td>
</tr>
<tr>
<td>Weight</td>
<td>${users.weight}</td>
</tr>
<tr>
<td>Height</td>
<td>${users.height}</td>
</tr>
<tr>
<td>Sport</td>
<td>${users.sport}</td>
</tr>
<tr>
<td>Place</td>
<td>${users.place}</td>
</tr>
</table>
</c:forEach>
</body>
</html>
When i'm starting my project and opening my JSP, i have no output from my DB. Help pls!

java.sql.SQLException: ORA-00942: table or view does not exist

While running a login program, I am using Apache Tomcat 7.0, JDK 7, JRE 7, Eclipse Juno, and Oracle Database 10g Express Edition
#login2.jsp
<body>
<div class="content">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Booking</li>
<li>Registration</li>
<li>Gallery</li>
<li>Packages</li>
<li>Logout</li>
</ul>
</div>
<div class="right">
<form action="Login2_back.jsp" method=post>
<table>
<tr>
<th>5 STAR HOTEL LOGIN</th>
</tr>
<tr>
<td>
<input type="text" name=n1 placeholder="User Id">
</td>
</tr>
<tr>
<td>
<input type="password" name=n2 placeholder="Password">
</td>
</tr>
<tr>
<td>
<input type="submit" name="action" value="SIGN IN">
</td>
</tr>
<tr>
<td align=center><b>NEW USER</b></td>
</tr>
<tr>
<td>
<center>REGISTER HERE </center>
</td>
</tr>
</table>
<tr>
<td>search
update </td>
</tr>
</form>
</div>
</div>
</body>
#Login2_back.jsp
<%#page import="java.sql.*" %>
<%#page import="pack.Dao" %>
<body>
<% try { Connection con=Dao.dbconnect(); Statement stmt=con.createStatement();
String z=r equest.getParameter( "n1");
String x=r equest.getParameter( "n2");
ResultSet rs=stmt.executeQuery( "select userid,password from login where userid='"+z+ "' and password='"+x+ "'"); i
f(rs.next()) { %>
<jsp:forward page="header1.jsp" />
<% } else System.out.println( "Sorry Invalid user and password"); %>
<jsp:forward page="header1.jsp" />
<% } catch(Exception e) {out.println(e);} %>
</body>
In cmd I connected to the database and I created a table login with `userid=sagar` and `password=sinha`
USERID PASSWORD
-------------------- --------
sagar sinha
after I commit;
But when I am going to login page using above code `login2.jsp`, using `userid:sagar` and `password:sinha`, I got the error
java.sql.SQLException: ORA-00942: table or view does not exist
#Dao.java
package pack;
import java.sql.Connection;
import java.sql.DriverManager;
public class Dao{
private static Connection con;
public static Connection dbconnect()
{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:orcl","hr","hr,"");
}catch (Exception e){
System.out.println(e);
//TODO: handle exception
}
return con;
}
}
This is the connection i used to connect to the database oracle 10g express edition but when i am login it shows
java.lang.NullPointerException..
i did not get the error why it is coming everthing is correct in the program and successfully connected to the database . sqlplus /nolog then conn hr/hr,, connected cr
This error is that you are making a query on a table or view that doesn't exists on your database schema,you have that login table?
error is pretty straight forward. table does not exist, check your DAO class and confirm the connection string if it is connecting to the right database, then from there see if the login table exist

Submitting form with multiple dropdown boxes?

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");

Categories