Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JDBC hit and view java

Have Programming Question? Ask it here!
 
 
 


Mumbai
hit and view java
1 Answer(s)      a year and 3 months ago
Posted in : JDBC


Q:hit and view servlet


Advertisement
View Answers

May 21, 2013 at 11:07 PM


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.mysql.jdbc.PreparedStatement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ignite178
 */
public class hit 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 processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet hit</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet hit at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * 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 {
        PrintWriter out=response.getWriter();
        String Id=request.getParameter("id");
        String Name=request.getParameter("name");
        String Home=request.getParameter("home");
        String Location=request.getParameter("location");
        String Gender=request.getParameter("sex");
        String Choice=request.getParameter("choice");

        try{
         Class.forName("com.mysql.jdbc.Driver");
         Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/location", "root", "root");
        String sql="INSERT INTO posting (ignite_id,name,home,location,gender,choice) VALUES('"+Id+"','"+Name+"','"+Home+"','"+Location+"','"+Gender+"','"+Choice+"')";
      PreparedStatement ps=(PreparedStatement) con.prepareStatement(sql);
        ps.executeUpdate();
        ps.close();
        con.close();



        }
        catch(Exception e)
        {
            out.print(e.toString());
        }
        finally{
            out.print("Submit Successfully");
        out.close();

        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
============================================
view
=================
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ignite178
 */
public class view extends HttpServlet {
    private ServletResponse response;

    /**
     * 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 processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet hit</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet hit at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * 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 {
//        try {
//            try {
//                Class.forName("com.mysql.jdbc.Driver").newInstance();
//            } catch (InstantiationException ex) {
//                Logger.getLogger(view.class.getName()).log(Level.SEVERE, null, ex);
//            } catch (IllegalAccessException ex) {
//                Logger.getLogger(view.class.getName()).log(Level.SEVERE, null, ex);
//            }
//        } catch (ClassNotFoundException ex) {
//            Logger.getLogger(view.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        Connection con = null;
//        try {
//            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/location", "root", "root");
//        } catch (SQLException ex) {
//            Logger.getLogger(view.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        String id=request.getParameter("id");
//        String query = "select * from posting where ignite_id='"+id+"'";
//        Statement s = null;
//        try {
//            s = con.createStatement();
//        } catch (SQLException ex) {
//            Logger.getLogger(view.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        ResultSet rs = null;
//        try {
//            rs = s.executeQuery(query);
//        } catch (SQLException ex) {
//            Logger.getLogger(view.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        request.setAttribute("data", rs);
//        RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
//        rd.forward(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 {
        PrintWriter out = response.getWriter();
        String Id = request.getParameter("id");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/location", "root", "root");
            String id=request.getParameter("id");
        String query = "select * from posting where ignite_id='"+id+"'";
        Statement s = null;
            s = (Statement) con.createStatement();
            ResultSet rs = null;
             rs = s.executeQuery(query);
              request.setAttribute("data", rs);
        RequestDispatcher rd = request.getRequestDispatcher("view.jsp");
        rd.forward(request, response);              

      } catch (Exception e) {
            out.print(e.toString());
        } finally {

            out.close();

        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}


Related Tutorials/Questions & Answers:
hit and view java
hit and view java  Q:hit and view servlet   //hit... javax.servlet.http.HttpServletResponse; /** * * @author ignite178 */ public class hit...;body>"); out.println("<h1>Servlet hit at " + request.getContextPath
Hit Counter Schema using java script - WebSevices
Hit Counter Schema using java script   Dear Sir, I am sending you none project i need you help please tell me how to do it? first I have to maintain a cookie, however me choose to do it. The cookie will be maintained
Advertisements
tree view - Java Beginners
tree view  I want to use tree view in my example code to display data... visit the following links: http://www.roseindia.net/java/example/java/swing/TreeComponent.shtml http://www.roseindia.net/java/example/java/swing
tree view - Java Beginners
time regarding java. I have another problem, Actually I want a tree view in my application which will display the files...   Hi Friend, You are using jsp or java swing. Please clarify this. Do
tree view - Java Beginners
tree view   I m using swing for the treeview in my application and also the database(Hsqldb). I want to connect the tree view with the database fields. If u want more clarification ,let me know. Thanks
view
view  what is the use view in database
grid view in java - Java Beginners
grid view in java  Is it possible to create dynamic grid view to display data from database. if yes. how ??   Hi Friend, Yes, you can. Try the following: import java.awt.*; import java.sql.*; import
Tree View with database - Java Beginners
Tree View with database   Hi, I'm working with Swing.I have to construct a SplitPane with JTree on the leftside and its details which is stored in the database on the right side of the pane. For ex: Network | |Node1
View source code of a html page using java ..
View source code of a html page using java ..  I could find the html source code of a web page using the following program, http://download.oracle.com/javase/1.4.2/docs/api/java/net/URLConnection.html i could get the html code
need view for jsf - Java Server Faces Questions
need view for jsf  Hi, iam using eclipse 3.1 and jsf 1.1 in my application,while creating only we can see the view of jsf page ,it will appear at left corner of eclipse,this is i saw in somewhere now i need this one please tell
Java Model View Controller (MVC) Design Pattern
; margin-left: 400px; } .style4 { margin-left: 160px; } Java MVC ( Model View Controller ) Design Pattern Model View controller is a classical design... logic and view who represents data. MVC design pattern isolates
Ask make graph from i report and view to java Language Programming
Ask make graph from i report and view to java Language Programming  Dear sir, Please help me, i want to make a graph from mysql to i report and view to java. Please help me, give me example source code please. thank you
when i hit enter it represent action like click a button
when i hit enter it represent action like click a button  Hi, Plz provide a program in html like I want 2 submit buttons,when i hit the enter button it will represent the action like click the 1st submit
How to find if you have been hit by Panda 4.0
How to find if you have been hit by Panda 4.0  Hi, This week Google... hit by Panda 4.0? Thanks   Hi, There are many ways you can find if your website have been hit by the Panda 4.0. Here are thinks you can do
View jsp
View jsp  <%@ page language="java" contentType="text/html; charset...; charset=ISO-8859-1"> <title>View ur Details</title> </head>...;%=request.getContextPath()%>/Controller"> <input type="hidden" name="page" value="view
tree view
tree view  display a tree view in flex?nodes will be in the form
RMS & View
RMS & View  DIffrence between RMS & VIEW
What is View?
What is View?  What is View?   Hi, A SQL View is a virtual table, which is based on SQL SELECT query. Essentially a view is very close...), except for the fact that the real tables store data, while the views don?t. The view
iOS View
iOS View   What is the Difference between View's bounds and frame in iOS?   Difference between View's bounds and frame in iOS The frame of a view is the rectangle, expressed as a location (x,y) and size (width,height
java programming code to upload a dataset(text file) and show the values in graphically view
java programming code to upload a dataset(text file) and show the values in graphically view  how can be done upload a dataset(text file) and show the values in graphically view,,related to slam benchmarking java code
Java Architecture Over View
patterns Java originally made its debut on client machine and browser. With increasing third party support in java 2 , J2EE (Java 2 Enterprise Application... their intranet, through a wired network or wireless network.   Java
View object in JSF
View object in JSF  What is view object
How to Extract row from table view using JSP Code - Java Beginners
How to Extract row from table view using JSP Code  Hi Friends, This is vinay here. Hope this mail finds u all in condition. I am totally new to java world and trying to design a web page using NetBeans IDE 6.8. My
View resolvers
In this section, you will learn about view resolvers in Spring
pre view of image uploaded
pre view of image uploaded  I am uploading a file through in html form. I want to display preview of that uploaded image in the same form using java script only.. and I want to insert that image into oracle database I don't want
pre view of image uploaded
pre view of image uploaded  I am uploading a file through input in html form. I want to display preview of that uploaded image in the same form using java script only.. and I want to insert that image into oracle database I
pre view of image uploaded
pre view of image uploaded  I am uploading a file through input type="file" in html form. I want to display preview of that uploaded image in the same form using java script only.. and I want to insert that image into oracle
Uploaded File View Page
Uploaded File View Page  hello, How to view the uploaded file with out downloading it into the machine by using JSP. Sharath
remove previous view iphone
remove previous view iphone  How to remove previous view in iPhone application on loading a new subview
Custom Cell View UITableview
Custom Cell View UITableview  How can i get two custom cell view in UITableView
view will appear iphone
view will appear iphone  Hi, What is the code for view will appear iphone? Thanks   HI, Here is the code: - (void)viewWillAppear:(BOOL...(@"View will appear"); } This method is executed once view is about to appear
view mapping - Hibernate
view mapping  How can we do mapping in hibernate
Iphone Table View Application
Iphone Table View Application  Hi, How to use Table View Application in Iphone? What parameters i have to follow to develop the Table view Apps with iphone? Please guide me or suggest any online example for reference. Thanks
adding view to viewcontroller
adding view to viewcontroller  How to add a frame window that opens on button click .. in iPhone application
SQL Alter View
SQL Alter View       View is a virtual and imaginary table come out as result -set... as real table. In SQL Alter View is used to modify a previous created view
Simple Table View With Next View
Simple Table View With Next View In this iphone tutorial will learn how to use Table View and also how to add object to table view, to insert the object into the table view we have to take an array and through that will add or insert
SQL Create View
SQL Create View       A View is a imaginary and virtual table in SQL. In SQL, a view is defined  as a virtual table outcome as a result of an SQL statement. A view
previous view controller
previous view controller  How to get back to the previous view... to go back to the previous view controller. See the example given below.. UINavigationController But in case if you have more then one root view controller
Table view with multiple view and sections
Table view with multiple view and sections Project will look like this :    In this tutorial we are going to add another view to the cell(Rows of Table View)  and will have two section in Table View
Height for cell in table view - UITableView
Height for cell in table view - UITableView  UITableview height for cell What is the height for cell in table view iPhone mobile application
fast view web--yes - Struts
fast view web--yes  How to enable the fast web view--yes on pdf file  Hi Friend, Please clarify your problem. Thanks
view data from jTextArea to jtable
view data from jTextArea to jtable  good night Please help senior java all, I want to make a brief program of reading data in the text area... this: I have a sentence like this **Java is a programming language that uses
RSS View
RSS View       RSS View is a news reader for RSS and Atom feeds designed to integrate... and restart Eclipse RSS View and FreeMem come with a new view that has
iphone Table View
iphone Table View In this tutorial will learn how to use Table View and also how to add object to table view, will add or insert the object into the Table View. Then will set the number of rows and the component into the Table View, when
iPhone Multiple View
iPhone Multiple View In this iphone tutorial we will show you multiple view, when you press button will take you to another view and also the button type will be changed according to the view, so that you can know that the view
Mysql Alter View
Mysql Alter View       View is a virtual table, act like a table but  actually it does not contain data. The view is the outcome result set of a SELECT statement
iPhone Simple Table View
Simple Table View In this tutorial will learn how to use Table View and also how to add object to table view, to insert the object into the table view we have... View. After that will set the title name for the navigation controller which
SQL Create View
SQL Create View       A View is a imaginary and virtual table in SQL. In SQL, a view is defined  as a virtual table outcome as a result of an SQL statement. A view
JSP: View Session
JSP: View Session          The view consists of the Html page or the jsp components that provide the user interface. View is what we see. It is mainly
Advertisements
 

 

 

DMCA.com