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>
Related
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
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"%>
I've created a login environment using jsp. index.jsp, login.jsp. I take username and password from database. If user name and password matches with the database it does login process perfectly. When user give wrong name or password it shows a error message invalid name or passwordand redirect to the login page. Nothing's wrong, but I am facing a problem when I login first. The place where the error message is shown after submitting wrong name or password that place is showing null.
Why null is showing?
Below is my code
index.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Example</title>
</head>
<body>
<form action="login.jsp" method="post">
<center>
<table border="1" width="30%" cellpadding="3">
<thead>
<tr>
<th colspan="2" align ="left">Login Here</th> <h5><%=request.getAttribute("errorMessage")%> </h5>
</tr>
</thead>
<tbody>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" value="" /></td>
</tr>
<tr>
<td><input type="submit" value="Login" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>
login.jsp
<%# page import ="java.sql.*" %>
<%
String userid = request.getParameter("uname");
String pwd = request.getParameter("pass");
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "postgres", "root");
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from member where uname='" + userid + "' and pass='" + pwd + "'");
if (rs.next()) {
session.setAttribute("userid", userid);
response.sendRedirect("success.jsp");
} else {
request.setAttribute("errorMessage", "Invalid user or password");
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
%>
You can use this in your index.jsp
<% if ((String) request.getAttribute("errorMessage") != null) {%>
<h3 style="color: red;"><%=(String) request.getAttribute("errorMessage")%></h3>
<%
}
%>
Instead of this
<%=request.getAttribute("errorMessage")%>
please check condition before showing error, if request parameter is null then it should not be rendered else it would be rendered, modify you code according to below snippet
<thead>
<tr>
<th colspan="2" align ="left">Login Here</th>
<h5>
<%if(request.getParameter("errorMessage")!=null){%>
<%=request.getParameter("errorMessage")%><%}%>
</h5>
</tr>
</thead>
The errorMessage attribute will not be available in request for the first time. Kindly check for a null and then display the message.
<%=request.getAttribute("errorMessage")%>
The below piece of code could help you.
<tr>
<th colspan="2" align ="left">Login Here</th>
<h5>
<%
String errorMsg = request.getAttribute("errorMessage");
if (errorMsg != null) {
%>
<%=errorMsg%>
<%
}
%>
</h5>
</tr>
As errorMessage attribute is null in the request when you load it first time, so you are getting null. You need to add null check for it. You can do this way
<th colspan="2" align ="left">Login Here</th>
<% if (request.getAttribute("errorMessage") != null) {
out.println("<h5>" + request.getAttribute("errorMessage") + "</h5>");
}%>
Correct your code in login.jsp
write session.setAttribute("errorMessage", "Invalid user or password");
instead of request.setAttribute("errorMessage", "Invalid user or password");
..
null message show because of this.
I have a JSP called create-account.jsp which collects a customer's details and sends the data to a Servlet. The Servlet inserts the customer's data into the database, queries the database add sets the results as request scoped attributes and the dispatches to create-account.jsp(same jsp). Now the inserted details must appear in a hidden html table in the jsp. How do I do it? Can any one help?
create-account.jsp
<!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=UTF-8">
<title>Create New Account</title>
<style>
a {
text-decoration: none;
}
body {
border-color: azure;
}
</style>
</head>
<body>
<p align="right">Hi, ${sessionScope.user.userName} logout</p>
<h2 align="center">Create New Account</h2>
<form action="${pageContext.request.contextPath}/create.do" method="post">
<table border="1" align="center">
<tr>
<td>Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>DOB:(yyyy-mm-dd)</td>
<td><input type="text" name="dob"></td>
</tr>
<tr>
<td>Balance:</td>
<td><input type="text" name="balance"></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="Create" name="create">
</td>
</tr>
</table>
</form>
<p align="center" style="display: none">${requestScope.result} has been successfully inserted into the table</p>
<table id="hiddenTable" style="display: none" border="1">
<tr>
<td>
</td>
</tr>
</table>
</body>
</html>
Use
style="visibility: hidden;"
or
style="display:none;"
to hide the table.
for more info have a look at below links
CSS Display and Visibility
Show/Hide div javascript
How to show the result in table?
sample code:
Servlet:
public class Model{
private String name;
private String dob;
private double balance;
// getter and setter
}
...
List<Model> list = new ArrayList<Model>();
//add the data in list
request.setAttribute("list",list);
JSP:
<c:if test="${not empty list}">
<c:forEach var="ob" items="${list}">
<tr>
<td><c:out value="${ob.name}"/></td>
<td><c:out value="${ob.dob}"/></td>
<td><c:out value="${ob.balance}"/></td>
</tr>
</c:forEach>
</c:if>
For complete code have a look at jsp iterate over list
i am posting my code in which i am getting the java.lang.NullPointerException exception.
<%# page contentType="text/html;charset=UTF-8" %>
<%# page errorPage="myError.jsp?from=customers.jsp" %>
<%# page import="java.sql.*" %>
<html>
<head>
<title>Insurance Quoting System</title>
</head>
<body>
<basefont face="Arial">
<!-- JSP Declarations -->
<%! ResultSet rs = null; %>
<!-- JSP Scriptlet -->
<%
try {
Class.forName("com.mysql.mysql.Driver");
Connection db = DriverManager.getConnection("jdbc:mysql://localhost:3306/quoting","root","root");
Statement s = db.createStatement();
rs = s.executeQuery("select * from customer");
}
catch (Exception e) {
// For now, just report the error to the system log
System.out.println(e.toString());
}
%>
<!-- Template text -->
<table width="550" border="0" align="center">
<tr>
<td bgcolor="#006633">
<div align="center">
<font size="6" color="#FFFFFF">
<b>Insurance Quoting System</b>
</font>
</div>
</td>
</tr>
<tr>
<td>
<p> </p>
<p> </p>
<p align="center"><b>Customers</b></p>
<table width="290" border="0" align="center">
<%
try {
while (rs.next()) {
%>
<!-- JSP Expressions used within template text -->
<tr>
<td width="20"><%= rs.getInt(1) %></td>
<td width="70"><%= rs.getString(2) %></td>
<td width="70"><%= rs.getString(3) %></td>
<td width="40">
<a href="custMaint.jsp?id=<%= rs.getString(1) %>&action=edit">
edit
</a>
</td>
<td width="40">
<a href="custMaint.jsp?id=<%= rs.getString(1) %>&action=delete">
delete
</a>
</td>
<td width="40">
<a href="custMaint.jsp?id=<%= rs.getString(1) %>&action=newQuote">
new quote
</a>
</td>
</tr>
<%
}
}
catch (SQLException e) {
// For now, just report the error to the system log
System.out.println(e.toString());
}
%>
</table>
</td>
</tr>
<tr>
<td>
<p> </p>
<p align="center">New Customer</p>
</td>
</tr>
</table>
</body>
</html>
i am making one jsp page its giving some error java.lang.NullPonterException on my jsp page pls tell me the ans if any one have .
The only thing I can think of that is causing this is that you are catching an exception in your first try-catch block and then trying to use the ResultSet as if it's been initialized, but it's still null, so you are getting NPE. Check to see if your Database operations work correctly.