Home Discussion Discuss: Java : Servlet Tutorials

  • servletsaritha June 22, 2011 at 10:43 AM

    excellent guide for java trainee

  • About this siteSupriya Patil July 4, 2011 at 11:23 AM

    Actually i am very thankful to you.this site is very good and very helpful.all the concepts are explained in detail.Thank u very very much.

  • web technologiessujatha March 8, 2013 at 3:42 PM

    i would like to know the process how i can set database drives to connect and access through servelt to process my application

  • dropdown example genderiica December 7, 2011 at 10:16 AM

    Here's my code. out.println("<td>Gender:</td>"); out.println("<td><select name='gender' ><option>"+rs.getString(7)+"</option><option>Male</option><option>Female</option></select></td>"); This displays the result and the gender option Male and Female. I want to know if I can use an if statement that when Female is selected as the gender, Male should be left in the drop down.

  • DulWUbNLspJonetta March 6, 2013 at 8:54 AM

    You've really helped me understand the isuses. Thanks.

  • advanced java(servlet)priyanka kumari April 18, 2012 at 6:49 PM

    i want to make online shopping as my final year b.tech project.there is no error came in my sevlet program during execution but output not give proply... i want retrive image with txt field from database (saved in binary format) and display in output.if we click any radio button corressponding image with txt displayed on output. forward1 page coding for this program ang image1 is for image retreving from database i am using mysql database,server tomcat6.........plz help for my final year project... /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package pkg; import com.mysql.jdbc.PreparedStatement; import java.sql.Blob; import java.sql.ResultSet; import java.sql.Statement; import java.sql.Connection; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.sql.SQLException; import java.sql.DriverManager; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.swing.JOptionPane; /** * * @author Priyanka */ public class forward1 extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream out = response.getOutputStream(); response.setHeader("cache-control", "no-store"); //response.setContentType("text/html;charset=UTF-8"); String str=request.getParameter("r1").trim(); // PrintWriter pw = response.getWriter(); try { out.println( "<html>"); out.println("<head>"); out.println("<title></title>"); out.println("<meta http-equiv=Content-Type content=text/html; charset=UTF-8>"); out.println ("<link rel=stylesheet type=text/css href=css/ss7.css/>"); out.println("</head>"); out.println("<body>"); out.println("<form action=buy method=get>"); out.println("<div id=top>"); out.println("<IMG src=img/PIC.JPG width=1250 height=150/>"); out.println("</div>"); Class.forName("com.mysql.jdbc.Driver"); java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","root"); java.sql.Statement st1=con.createStatement(); Statement statement = con.createStatement(); PreparedStatement ps=null; //String cmd=""; if(str.equals("ladies_wear")) { ps=(PreparedStatement) con.prepareStatement("select * from wear"); } else if(str.equals("ladies_footwear")) { ps=(PreparedStatement) con.prepareStatement("select * from footwear"); } else if(str.equals("wrist_watches")) { ps=(PreparedStatement) con.prepareStatement("select * from wrist_watches"); } else if(str.equals("jewellery")) { ps=(PreparedStatement) con.prepareStatement("select * from jewellery"); } else if(str.equals("cosmetics")) { ps=(PreparedStatement) con.prepareStatement("select * from cosmetics");; } else if(str.equals("home_appliance")) { ps=(PreparedStatement) con.prepareStatement("select * from home_appliance"); } else if(str.equals("Ladies_Bag")) { ps=(PreparedStatement) con.prepareStatement("select * from ladies_bag"); } //ResultSet rs=st1.executeQuery(cmd); //Blob image=null; ResultSet resultset=ps.executeQuery(); if(resultset.next()) { out.print("<center>"); out.print("<table border=2 width=1000>"); //int bufferSize = 1024; out.println("<tr>"); out.println("<th>"); out.println("productid"); out.println("</th>"); out.println("<th>"); out.println("status"); out.println("</th>"); out.println("<th>"); out.println("price"); out.println("</th>"); out.println("<th>"); out.println("description"); out.println("</th>"); out.println("<th>"); out.println("picture"); out.println("</th>"); out.println("<th>"); out.println("categories"); out.println("</th>"); out.println("<th>"); out.println("size"); out.println("</th>"); out.println("<th>"); out.println("Choose"); out.println("</th>"); out.println("</tr>"); while(resultset.next()) { out.print("<TR>"); String str1=resultset.getString(1); out.print("<TD>"+str1+"</TD>"); out.print("<TD>"+resultset.getString(2)+"</TD>"); out.print("<TD>"+resultset.getString(3)+"</TD>"); out.print("<TD>"+resultset.getString(4)+"</TD>"); out.print("<TD><img src='image1?proid="+str1+"' width='50' height='50' /></TD>"); out.print("<TD>"+resultset.getString(6)+"</TD>"); out.print("<TD>"+resultset.getString(7)+"</TD>"); out.println("<TD>"); out.println("<input type=checkbox name="+resultset.getString(1)+">"); out.println("</TD>"); out.print("</TR>"); //} } out.print("</TABLE>"); out.println(" <div id=bottom align=center>"); HttpSession ses=request.getSession(true); ses.setAttribute("cat", str); String name=(String)ses.getAttribute("uname"); //out.println(name); //out.println("<input type=hidden name=h1 value="+name+">"); out.println("<input type=submit value=ADD TO CART >"); out.println("</div>"); out.println("</form>"); out.println("</body>"); out.println("</html>"); } } catch(Exception e){out.print(e.getMessage());} } } image1 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package pkg; import java.io.*; import java.sql.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author S O M D E B */ public class image1 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { InputStream sImage; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con = DriverManager.getConnection("jdbc:mysql://localhost/bank", "root", "root"); // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Connection con=DriverManager.getConnection("jdbc:odbc:picdemo","sa","niit"); PreparedStatement psmnt=null; String str=request.getParameter("r1"); if(str.equals("ladies_wear")) { psmnt = (PreparedStatement)con.prepareStatement("SELECT picture FROM wear where productid=?"); } else if(str.equals("ladies_footwear")) { psmnt = (PreparedStatement)con.prepareStatement("SELECT picture FROM footwear where productid=?"); } psmnt . setString(1, request.getParameter("proid")); ResultSet rs = psmnt.executeQuery(); if(rs.next()) { byte[] bytearray = new byte[1048576]; int size=0; sImage = rs.getBinaryStream(1); response.reset(); response.setContentType("image/jpeg"); while((size=sImage.read(bytearray))!= -1 ) { response.getOutputStream().write(bytearray,0,size); } } }catch(Exception ex){} } }

  • code for exception handlingVaishu June 14, 2012 at 11:12 PM

    // Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; // Extend HttpServlet class public class ErrorHandler extends HttpServlet { // Method to handle GET method request. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Analyze the servlet exception Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception"); Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name"); if (servletName == null){ servletName = "Unknown"; } String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri"); if (requestUri == null){ requestUri = "Unknown"; } // Set response content type response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Error/Exception Information"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n"); if (throwable == null && statusCode == null){ out.println("<h2>Error information is missing</h2>"); out.println("Please return to the <a href=\"" + response.encodeURL("http://localhost:8080/") + "\">Home Page</a>."); }else if (statusCode != null){ out.println("The status code : " + statusCode); }else{ out.println("<h2>Error information</h2>"); out.println("Servlet Name : " + servletName + "</br></br>"); out.println("Exception Type : " + throwable.getClass( ).getName( ) + "</br></br>"); out.println("The request URI: " + requestUri + "<br><br>"); out.println("The exception message: " + throwable.getMessage( )); } out.println("</body>"); out.println("</html>"); } // Method to handle POST method request. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

  • servletkrishan suryavanshi July 21, 2012 at 2:28 PM

    hoow to upload file and image in oracle database with servlet.

  • vhdVzUIIvBJoyce June 15, 2013 at 4:25 PM

    That insgiht's just what I've been looking for. Thanks!

  • Java servlet Ankit October 20, 2012 at 12:00 AM

    Here, This site provides very useful information for java begineers to learn the every topic in deatil. You can find article for Java Servlet here.. http://techdistance.com/java-servlet-basic-tutorial-with-example/

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Related Articles