I'm trying to insert data into database through a JSP page and a servlet. I have to store a timestamp value like this :"2016-FEB-12 10:45:22". When I try to enter the data into the database I'm being thrown the following error : "oracle.net.ns.NetException: Size Data Unit (SDU) mismatch".
This is my JSP PAGE:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Bid Form</title>
</head>
<body style="background:color=LightGreen">
<form action="BidInsert" method="post">
<table border=1 align="center">
<tr>
<th>Bid Number</th>
<td><input type="text" name="bid_no"></td>
</tr>
<tr>
<th>Amount</th>
<td><input type="text" name="amount"></td>
</tr>
<tr>
<th>User Id</th>
<td><input type="text" name="u_id"></td>
</tr>
<tr>
<th>Listing Id</th>
<td><input type="text" name="l_id"></td>
</tr>
<tr>
<th>Time stamp Info</th>
<td><input type="text" name="timestampinfo" size=50></td>
</tr>
</table>
<center>
<input type="submit" value="submit">
</center>
</form>
</body>
</html>
This is my Servlet:
package Serve;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class BidInsert
*/
public class BidInsert extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public BidInsert() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out= response.getWriter();
response.setContentType("text/html");
String bn,a,ui,li, tsi;
bn=request.getParameter("bid_no").toString();
a=request.getParameter("amount").toString();
ui=request.getParameter("u_id").toString();
li=request.getParameter("l_id").toString();
tsi=request.getParameter("timestampinfo");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c=DriverManager.getConnection("jdbc:oracle:thin:#apollo.vse.gmu.edu:1521:ite10g","","");
String sql="insert into Bid values('"+bn+"','"+a+"','"+ui+"','"+li+"','"+tsi+"')";
PreparedStatement ps=c.prepareStatement(sql);
ps.executeUpdate(sql);
out.println("Data Inserted successfully");
} catch (SQLException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(out.checkError()==false)
{
out.println(" <input type=\"button\" value=\"Check Database\" onClick=\"window.location.href('Database contents.jsp')\"> ");
}
}
}
I've omitted the username & password of the database intentionally.Is there are any specific data type to be used to take timestamp values?.Please suggest if any. Any help will be much appreciated.
For a start consider using a PreparedStatement (or similar) to avoid create sql insert strings - which are open to sql injection attacks.
Secondly it looks like the time info should be inserted as a TIMESTAMP or DATE, so you need to convert the String into a Date using SimpleDateFormat
see https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
Search SO and you can find thousands of examples
Related
I made my first Java Servlet application with JSPs to create a record about books and display them. The index.html page loads well but the other pages in the project don't work when deployed on GoogleAppEngine. The same project works well when run locally on App Engine through eclipse. I have also enabled sessions in the "appengine-web.xml" but the problem persists. The application works perfectly fine when run locally.
Running through the app engine I get the error -
Error: Not Found
The requested URL /BookApp was not found on this server.
Directory Structure
Servlet code :
package com.nh;
import com.books.Book;
import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class BookApp
*/
#WebServlet("/BookApp")
public class BookApp extends HttpServlet {
private static final long serialVersionUID = 1L;
ArrayList<Book>Books = new ArrayList<Book>();
static int id = 0;
/*List<String> BookNames = new ArrayList<String>();
List<String> Authors = new ArrayList<String>();
List<String> Costs = new ArrayList<String>();*/
/**
* #see HttpServlet#HttpServlet()
*/
public BookApp() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
String bookName = request.getParameter("bookname");
String author = request.getParameter("authorname");
String bookCost = request.getParameter("cost");
String url = ("");
HttpSession session=request.getSession(true);
Book newBook = new Book();
if(bookName.length()!=0&&author.length()!=0&&bookCost.length()!=0)
{
newBook.setAuthorName(author);
newBook.setName(bookName);
newBook.setCost(Float.parseFloat(bookCost));
newBook.setId(id++);
Books.add(newBook);
}
session.setAttribute("Books", Books);
request.setAttribute("Books", Books);
System.out.println(Books.get(0).getName());
url = ("/listBooks.jsp");
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Create a book entry</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Create A Book</h2>
<form action="BookApp" method="post">
<div class="form-group">
<label for="Title">Title:</label>
<input type="text" class="form-control" id="tbBook" placeholder="Enter Title" name="bookname" required>
</div>
<div class="form-group">
<label for="author">Author:</label>
<input type="text" class="form-control" id="authorname" placeholder="Enter Author" name="authorname" required>
</div>
<div class="form-group">
<label for="cost">Cost:</label>
<input type="text" class="form-control" id="tbCost" placeholder="Enter Cost" name="cost" required>
</div>
<button type="submit" class="btn btn-default">Create</button>
</form>
</div>
</body>
</html>
listBook.jsp :
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="com.books.Book" %>
<%# page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>List of books</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Book List</h2>
<table class="table table-hover">
<thead>
<tr>
<th>Sr No.</th>
<th>Title</th>
</tr>
</thead>
<tbody>
<%
ArrayList<Book> posts=(ArrayList<Book>) request.getAttribute("Books");
for (Book book: posts) {
%>
<%session.setAttribute("Books", posts); %>
<tr>
<td><%=book.getId()+1 %></td>
<td><%=book.getName()%></td>
</tr>
<%}%>
</tbody>
</table>
</div>
</body>
</html>
appengine-web.xml
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<threadsafe>true</threadsafe>
<sessions-enabled>true</sessions-enabled>
<runtime>java8</runtime>
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>
</appengine-web-app>
As you're using Java8 (<runtime>java8</runtime>) and as per Google App engine specification try implementing this way :
// With #WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
#WebServlet(name = "requests", description = "Requests: Trivial request", urlPatterns = "/requests")
public class RequestsServlet extends HttpServlet {
#Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
}
}
Reference : how-requests-are-handled-app-engine
One more advice : do not use camelCase while naming your url pattern.
Hello I know That this question has been asked before But unfortuanetly I didn t find among the answers proposed the one suitable for me
I m still new with J2ee lookin' forward for ur help
This is my code
login.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>Login page</title>
</head>
<body>
<form action="" method="post"/>
<br> UserId: <input type="text" name="userId"/>
<br> password<input type="password" name="password"/>
<br><input type="submit"/>
</form>
</body>
</html>
LoginServlet.java
package org.islem.login;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.islem.login.service.LoginService;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String userId,password;
userId= request.getParameter("userId");
System.out.println(userId);
password= request.getParameter("password");
LoginService loginService= new LoginService();
boolean result= loginService.authenticate(userId, password);
if (result)
{
response.sendRedirect("home.jsp");
return;
}
else
{
response.sendRedirect("login.jsp");
return;
}
}
}
LoginService.java
package org.islem.login.service;
public class LoginService {
public boolean authenticate (String userId,String password)
{
if (password ==null || password.trim() =="") {
return false;
}
else return true;
}
}
Your form action is empty, you should fill it your servlet name in your case it should be like:
<form action="LoginServlet" method="post"/>
I assume you already define your servlet name at your web.xml
You need to specify some controller name within the action:
<form action="LoginServlet " method="post"/>
<br> UserId: <input type="text" name="userId"/>
<br> password<input type="password" name="password"/>
<br> <input type="submit"/>
</form>
Remember: This action specifies where the data submitted by the form will be received.
I had tried from servlet set value by setattribute and it have to iterate in jsp by getattribute by setter and getter method without EL and JSTl but am getting this error .kindly help on this i searched in google but i cant found.
java.lang.NullPointerException
org.apache.jsp.Home_jsp._jspService(Home_jsp.java:138)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
here serlvet code
package servlet2;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.TreeSet;
import javafx.css.PseudoClass;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import servlet.Dao;
import jdbc.jdbcconnection;
/**
* Servlet implementation class Homepage
*/
//#WebServlet(asyncSupported = true, urlPatterns = { "/Homepage" })
public class Homepage extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public Homepage() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// TODO Auto-generated method stub
System.out.println("inside==========>");
String name=request.getParameter("name");
String password=request.getParameter("password");
String phone=request.getParameter("Phone");
String Deptmart=request.getParameter("Dep");
String gender=request.getParameter("gender");
String country=request.getParameter("country");
Dao d=new Dao();
try
{
jdbcconnection jc=new jdbcconnection();
Connection con=jc.getconnection();
String sql="insert into homepage(name,password,phone,Dept,gender,Country) values(?,?,?,?,?,?)";
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
ps.setString(3, phone);
ps.setString(4, Deptmart);
ps.setString(5, gender);
ps.setString(6, country);
System.out.println("ps=========>"+ps);
ps.execute();
con.commit();
String select="select * from homepage";
PreparedStatement ps1=con.prepareStatement(select);
ResultSet rs=ps.executeQuery();
ArrayList t=new ArrayList();
while(rs.next())
{
d.setid(rs.getInt(1));
d.setName(rs.getString(2));
d.setPassword(rs.getString(3));
d.setPhone(rs.getInt(4));
d.setDeptmart(rs.getString(5));
d.setGender(rs.getString(6));
d.setCountry(rs.getString(7));
t.add(d);
}
request.setAttribute("users",t);
RequestDispatcher rs1=request.getRequestDispatcher("/Home.jsp");
rs1.forward(request, response);
}
catch (SQLException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Jsp page:
<%#page import="servlet.Dao,java.util.*" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Homepage" method="get">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="text" name="password" id="password"></td>
<tr>
<td>Phone:</td>
<td><input type="text" name="Phone" id="Phone">
</tr>
</td>
<tr>
<td>Department:</td>
<td><select id="Dep" name="Dep">
<option value="it">IT</option>
<option value="finace">Finace</option>
<option value="market">Marketing</option>
</select></td>
</tr>
<tr>
<td>Gender:</td>
<td><input type="radio" name="gender" id="gender" Value="Male">Male
<input type="radio" name="gender" id="gender" Value="Female">Female</td>
</tr>
<tr>
<td>Country:</td>
<td><input type="checkbox" name="country" id="country"
Value="India">India <input type="checkbox" name="country"
id="country" Value="Other">Other</td>
</tr>
<br>
<tr>
<td><input type="submit" name="Add" Value="ADD" ></td>
<td><input type="button" name="Clear" Value="CLEAR"></td>
</tr>
</table>
</form>
<table border="1">
<tr><td>Id</td><td>name</td>
<td>Password</td>
<td>Phone</td>
<td>Deptmart</td>
<td>Gender</td>
<td>country</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<%
try
{
List<Dao> al1 = (List) request.getAttribute("users");
// System.out.println(al1); // prints null
for(Dao user : al1) {
%>
<tr>
<td><%=user.getid() %></td>
<td><%=user.getName() %></td>
<td><%=user.getPassword() %></td>
<td><%=user.getPhone() %></td>
<td><%=user.getDeptmart() %></td>
<td><%=user.getGender() %></td>
<td><%=user.getCountry() %></td>
<td></td>
<td></td>
</tr>
<%} }
catch(Exception e)
{
e.printStackTrace();
}
%>
</table>
</body>
</html>
web.xml
<servlet>
<servlet-name>Homepage</servlet-name>
<servlet-class>servlet2.Homepage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Homepage</servlet-name>
<url-pattern>/Homepage</url-pattern>
</servlet-mapping>
I'm new to mysql and teaching myself the ropes. I've come across a problem and hoping for some advice.
I created a form in a jsp page to insert data to a mysql database. All data is properly inserted, and when I do select* all data is properly displayed in a table I created. My problem is with my update page.
When I call data from the database back into a form on my editdata.jsp page, only everything before a space is returned. For example, I put the name 'Tom Jones' into a database, but when I go to retrieve it from the name field, only 'Tom' is returned; 'Jones' is not, but is still there in the database.
Here's my editdata.jsp code:
<%#page import="java.sql.ResultSet"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action ="UpdateData" method ="post">
<table border ="1" width="80%">
<%ResultSet res = (ResultSet) request.getAttribute("EditData");%>
<%if(res.next()){
%>
<tr>
<td>ID</td>
<td><input type="text" name="id" value=<%=res.getString("id")%>>
</td>
</tr>
<tr>
<td>Container Number </td>
<td><input type="text" name="id" value=
<%=res.getString("containerNumber")%> ></td>
</tr>
<tr>
<td>Size </td>
<td><input type="text" name="id" value=<%=res.getString("size")%>>
</td>
</tr>
<tr>
<td>Vessel </td>
<td><input type="text" name="id" value=<%=res.getString("vessel")%>>
</td>
</tr>
<tr>
<td>Full Out Date </td>
<td><input type="text" name="id" value=
<%=res.getString("fullOut")%>> </td>
</tr
<tr>
<td>Empty In Date </td>
<td><input type="text" name="id" value=<%=res.getString("emptyIn")%>
></td>
</tr>
<tr>
<td>Empty Out Date </td>
<td><input type="text" name="id" value=
<%=res.getString("emptyOut")%>> </td>
</tr>
<tr>
<td>Full In Date </td>
<td><input type="text" name="id" value=<%=res.getString("fullIn")%>>
</td>
</tr>
<tr>
<td>Comments </td>
<td><input type="text" name="id" value=
<%=res.getString("comments")%>> </td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Update" name="update"> </td>
</tr>
<%}%>
</table>
</form>
</body>
</html>
And my EditRecord servlet:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class EditRecord extends HttpServlet {
Connection conn;
ResultSet res;
Statement stmt;
String id, query;
DatabaseConnection dbconn;
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
id = request.getParameter("id");
dbconn = new DatabaseConnection();
conn = dbconn.setConnection();
stmt = conn.createStatement();
query = "select * from inventory where id = "+id;
res = dbconn.getResult(query, conn);
}catch(Exception e){
}
finally {
request.setAttribute("EditData", res);
RequestDispatcher rd = request.getRequestDispatcher("editdata.jsp");
rd.forward(request, response);
out.close();
}
}
/**
* Handles the HTTP <code>GET</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* #return a String containing servlet description
*/
#Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Again, the long and short of my problem is that full strings of data are not returned after spaces. So 'TOM JONES' only returns 'TOM' to the form above for editing. How do I get the full string of data to be returned. I don't know if it matters, but I used VARCHAR for all my record info.
Thanks for any and all help.
Attributes inside HTML tags are separated by spaces. That's why you're supposed to use quotes around the attribute values.
BAD:
<input type="text" name="id" value=<%=res.getString("comments")%>>
GOOD:
<input type="text" name="comments" value="<%=res.getString("comments")%>" />
I have three jsp GetAllEmployee,EmployeeFindByid,EmployeeFindByName ,now i have to code for EmployeeFindById ,EmployeeFindByName in one jsp, i have provide the button for all three in home page (index.jsp) as the user click on employeeFindbyid new pop window open and demanding for id after click on submit button output should be populate in index.jsp i have done with GetAllEmployeei m stucking in portion of EmployeeFindByid,EmployeeFindByName cause i have to write these in one jsp page as the user click on button one pop window should be open and taking the value then pop window should be closed automatically. I am not able to provide the conditon how to execute particular section of code of one jsp
this is my servlet package com.nousinfo.tutorial;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.nousinfo.tutorial.employee.service.model.bo.EmployeeBO;
import com.nousinfo.tutorial.service.bo.EmployeeService;
import com.nousinfo.tutorial.service.bo.impl.EmployeeServiceImpl;
import com.nousinfo.tutorial.service.exception.EmployeeServiceBOException;
import com.nousinfo.tutorial.service.exception.EmployeeServiceException;
/**
* Servlet implementation class GetEmployee
*/
public class GetEmployee extends HttpServlet {
private static final long serialVersionUID = 1L;
EmployeeService employeeService = new EmployeeServiceImpl();
/**
* #see HttpServlet#HttpServlet()
*/
public GetEmployee() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#service(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String name = request.getParameter("findbyid");
System.out.println(name);
RequestDispatcher dispatcher = null;
try {
if (request.getParameter("findbyid") != null) {
request.setAttribute("EmployeeObject", findEmployees(Long
.parseLong(request.getParameter("findbyid"))));
dispatcher = request.getRequestDispatcher("/jsp/employee.jsp");
} else {
request.setAttribute("employeeList", getAllEmployee());
dispatcher = request.getRequestDispatcher("/jsp/winopen.jsp");
}
if (dispatcher != null)
dispatcher.forward(request, response);
} catch (EmployeeServiceBOException e) {
e.printStackTrace();
}
}
private List<EmployeeBO> getAllEmployee() throws EmployeeServiceBOException {
return employeeService.getAllEmployees();
}
private EmployeeBO findEmployees(long id) throws EmployeeServiceBOException {
return employeeService.getEmployee(id);
}
private List<EmployeeBO> findEmployees(String name)
throws EmployeeServiceBOException {
return employeeService.findEmployees(name);
}
}
this is my index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<link rel="stylesheet" href="../css/style.css" type="text/css"></link><title>Home</title>
</head>
<body>
<table width="951" height="116" border="0" align="center">
<tr>
<td width="661" height="112" align="center" bgcolor="#99CCFF"><h2>Project Management </h2></td>
<td width="266" height="112" align="center" bgcolor="#FFFFFF"><img src="../image/nous.jpg" alt="1" width="266" height="84" /></td>
</tr>
</table>
<p> </p>
<table width="949" height="183" border="0" align="center">
<tr>
<td height="42" align="center" bgcolor="#3366FF"><strong>Find Employee </strong></td>
</tr>
<tr>
<td height="43"><form id="form2" method="post" action="/EmployeeWeb/GetEmployee">
<input name="Submit2" type="submit" class="button" value="GetAllEmployee"/>
</form> </td>
</tr>
<tr>
<td width="943" height="43">
<input name="Submit" type="submit" value="getEmployee By ID"
onClick="window.open('file.jsp','mywindow','width=500,height=200 align=center,toolbar=no,resizable=yes,menubar=yes' )" />
</td>
</tr>
<tr>
<td height="43">
<input name="Submit2" type="submit" class="button" value="Get Employee By Name" onClick="window.open('winopen.jsp','mywindow','width=500,height=350,toolbar=no,resizable=yes,menubar=yes')"/>
</td>
</tr>
</table>
<p> </p>
</body>
</html>
this is my getAllEmployee.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%#page language="java" import="com.nousinfo.tutorial.employee.service.model.bo.*" %>
<%#page language="java" import="java.util.*" %>
<input name="employee id" value="">
<table >
<tr>
<th>EmployeeNumber</th>
<th>FirstName</th>
<th>LastName</th>
<th>Title</th>
<th>Address-1</th>
<th>Address-2</th>
<th>City</th>
<th>State</th>
<th>Pincode</th>
<th>Mobile_Number</th>
<th>Date_Of_Birth</th>
</tr>
<c:forEach var="employee" items="${employeeList}">
<tr>
<td>${employee.empNumber}</td>
<td>${employee.firstName}</td>
<td>${employee.lastName}</td>
<td>${employee.title}</td>
<td>${employee.address1}</td>
<td>${employee.address2}</td>
<td>${employee.city}</td>
<td>${employee.state}</td>
<td>${employee.pincode}</td>
<td>${employee.mobileNUmber}</td>
<td>${employee.dateOfBirth}</td>
</tr>
</c:forEach>
</table>
</body>
</html>