Calling Java Serialized Object from JSP
This article is an answer for my simple assignment i created several weeks ago for my friend, Zainul Iman. Its about creating a simple web application that can persist and retrieve values by putting it values on a java object and serialized it on a plain text file.
Okay, so basically i only create 4 files. Only 1 java files, 2 jsp and 1 servlet. And only adding several lines on web.xml file.
First is a simple java bean to store my inserted value,
package com.edw.bean; import java.io.Serializable; public class Student implements Serializable{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
And a simple jsp file to serve as view layer, in here user can insert value which will be persist on java bean.
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert Value</title> </head> <body> <form method="post" action="StudentServlet"> <table> <tr> <td>Insert Name : </td> <td><input type="text" name="name" /> </td> </tr> <tr> <td colspan="2"><input type="submit" /></td> </tr> </table> </form> </body> </html>
This is my servlet class, in this class i handle the serialization of the inserted value into java object
package com.edw.servlet; import com.edw.bean.Student; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class StudentServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); Student student = new Student(); student.setName(name); FileOutputStream fileOutputStream2 = new FileOutputStream(new File("D:\\test12.txt"), false); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream2); objectOutputStream.writeObject(student); response.getWriter().println("Success....!!"); response.getWriter().println("list of student ---> <a href=\"\">daftar.jsp</a>"); } }
And the serialized object will be read from “daftar.jsp” file,
<%@page import="com.edw.bean.Student"%> <%@page import="java.io.ObjectInputStream"%> <%@page import="java.io.File"%> <%@page import="java.io.FileInputStream"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Daftar Nama</title> </head> <body> <h1>Registered Name is : </h1> <% try{ FileInputStream fileInputStream = new FileInputStream(new File("D:\\test12.txt")); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Student student = (Student)objectInputStream.readObject(); out.print("<h1>"+student.getName()+"</h1>"); }catch(Exception ex){ out.print(ex); } %> </body> </html>
Dont forget to register you servlet to web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>StudentServlet</servlet-name> <servlet-class>com.edw.servlet.StudentServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>StudentServlet</servlet-name> <url-pattern>/StudentServlet</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Well it’s not too hard isnt it 😉
3 Comments
Manish Mishra
about 6 years agoIt's too easy to understand .
ReplyManish Mishra
about 6 years agoI am so sorry for "too easy to understand" . I want to say you it's really very easy.
Replyedwin
about 6 years agoHi Manish, glad it can help ;)