Posts

Showing posts from May, 2011

Java Code to Create MenuBar in Applet

import javax.swing.JApplet; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class MenuApplet extends JApplet { public void init() { JMenuBar menubar = new JMenuBar(); JMenu menuFile = new JMenu("File"); JMenuItem openItem = new JMenuItem("Open"); menuFile.add(openItem); JMenuItem saveItem = new JMenuItem("Save"); menuFile.add(saveItem); menubar.add(menuFile); JMenu menuHelp = new JMenu("Help"); JMenuItem aboutItem = new JMenuItem("About"); menuHelp.add(aboutItem); menubar.add(menuHelp); setJMenuBar(menubar); } }

Servlet Code to Display Image from Database

import java.sql.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DisplayImage extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ String connectionURL = "jdbc:odbc:test"; java.sql.Connection con=null; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); con=DriverManager.getConnection(connectionURL); Statement st1=con.createStatement(); ResultSet rs1 = st1.executeQuery("select image1 from table1 where ID=8"); String imgLen=""; if(rs1.next()) { imgLen = rs1.getString(1); } rs1 = st1.executeQuery("select image1 from table1 where ID=8"); if(rs1.next()){ int len = imgLen.length(); byte [] rb = new byte[len]; InputStream readImg = rs1.getBinaryStream(1); int index=readImg.read(rb, 0, len);...

Servlet Code to Insert Image into Database

import java.sql.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ImageInsertInTable extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); String connectionURL = "jdbc:odbc:test"; Connection con=null; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); con = DriverManager.getConnection(connectionURL); PreparedStatement ps = con.prepareStatement("INSERT INTO table1 VALUES(?,?)"); File file = new File("C:/Documents and Settings/MOHSIN/Desktop/WebApplication4/web/Wallpaper.jpg"); FileInputStream fs = new FileInputStream(file); ps.setInt(1,8); ps.setBinaryStream(2,fs,fs.available()); int i = ps.executeUpdate(); pw.print("<h1>"+ i +"</h1>"); if(i!=0){ pw.prin...

Every JSP is a Servlet

Every JSP page is compiled into a servlet by JSP engine. The first time the JSP engine receives the request for a JSP, jsp engine converts the jsp files in to java servlets and than compiles this servlet, it is called the translation phase. If needed JSP pages can also be precompiled. Once the JSP page is compiled into a servlet, all the subsequent requests will be handled by the compiled servlet class. If you modify the source code of the JSP file, container automatically detects the changes and recompile the JSP page the next time that JSP page is requested. Note: If the JSP pages are translated on demand when the container receives the request for an untranslated JSP page, then there will be a slow response for the first time any JSP page is requested. On other hand container may choose to translate the JSP pages at deployment time in that case the first request for any JSP page will take the normal time.

How does JSP differs from Servlets

From the developers perspective Servlets are pure java programs with class and method definitions whereas a JSP page is much like a text document or web page. With servlets developer must write java code to output the entire markup, on the other hand a JSP page can be designed like a static web page. JSP separates static content from dynamic content and Java beans and/or custom JSP tags are used to generate dynamic portion of the web page. Servlets are well suited for handling client request and executing application logic whereas JSP pages are well suited as views.

JSP code for "Display" Info from Database

<%@page language="java" import="java.sql.*"%> <html> <body> <h3>Mohsin Sayad <%!ResultSet rs=null;;Statement st=null;;Connection cn=null; %> <% try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); cn=DriverManager.getConnection("jdbc:odbc:test"); st=cn.createStatement(); rs=st.executeQuery("select * from BCASTUDENT_TEMP"); while(rs.next()) { out.print(rs.getString(1)); out.print(rs.getString(2)); } } catch(Exception ee) { out.println(ee); } %> </h3>> </body> </html>