tags:

views:

37

answers:

3

I dont know JSP. I have written the below Java JDBC code which must be integrated in JSP.

import java.net.URL;
import java.net.URLConnection;
import java.sql.*;
public class searchlink{
public static void main(String args[]) throws Exception {
    Connection con=null;
    Statement stmt=null;
    Statement stmtR=null;
    String link="http://www.topix.com/rss/city/ellensburgwa";
    String source="Sample";
    if(con==null){
            SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK");
            con=SQLConnection.getNewConnection();
            stmt=con.createStatement();
            stmtR=con.createStatement();
    }
    ResultSet rs;
    boolean hasRows=false;
    rs=stmt.executeQuery("select url from urls where url='"+link+"'");
    while(rs.next()){
        hasRows=true;
        //String mem=rs.getString(1);
        System.out.println("This URL already exists in DB");}
    if (!hasRows)
    {
        PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urls VALUES(?, ?, ?, ?, ?)");
        insertUrlStatement.setInt(1, 21211);
        insertUrlStatement.setString(2, link);
        insertUrlStatement.setString(3, source);
        insertUrlStatement.setInt(4, 1);
        insertUrlStatement.setInt(5, 0);
        insertUrlStatement.executeUpdate();
             }
           }
        }

Initially, my task is to create a text box and assign the value the user enters in it to the String named link which is in the code above.

Kindly advise how to build a JSP program for that. Also, please let me know whether any changes are to be made in the Java code above for integrating in JSP or I can just include the whole program inside <% content %>.

+3  A: 

First of all, you should avoid writing Java code in a JSP file.

Here's a step by step:

  1. Learn at least HTTP and HTML. To the point, you need to understand what HTTP is all about and distinguish the "server side" and "client side" concepts. You also need to learn HTML as web markup language and the HTML forms to collect user input. This answer contains helpful links.

  2. Learn JSP and Servlets. You need to understand that JSP is a Java based view technology which provides a template to write HTML/CSS/JS in which in turn will be sent to the webbrowser. This answer contains helpful links.

  3. Having learnt 1 and 2, start creating a JSP file with a simple HTML form with the necessary input fields:

     <form action="servleturl" method="post">
         <input type="text" name="link">
         <input type="text" name="source">
         <input type="submit">
     </form>
    
  4. Then create a class which extendsHttpServlet which gathers those input parameters and saves in the database in the doPost() method:

     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         String link = request.getParameter("link");
         String source = request.getParameter("source");
         LinkDAO dao = new LinkDAO();
         try {
             dao.save(link, source);
         } catch (SQLException e) {
             throw new ServletException("Saving in DB failed", e);
         }
         request.getRequestDispatcher("result.jsp").forward(request, response);
     }
    

    Map this servlet in web.xml file on an url-pattern of servleturl (at least, it should be the same as where your <form action> is pointing to).

  5. Then create a DAO class which does the necessary JDBC stuff:

     public class LinkDAO {
         public void save(String link, String source) throws SQLException {
             // Your original code here. You should however modify it to make it
             // free of potential resource leaking.
         }
     }
    

    To learn a bit more how to get started with the DAO pattern, you may find this article useful.

BalusC
A: 

Time to go learn some basics:

The Model-View-Control (MVC) pattern would be a very good start. You are in luck - there are many implementations in Java:

http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller#Java

Pablojim
A: 

if you get past that hurdle id also suggest you use something like springs JdbcDaoSupport to manage your datasource connections. As well as cutting down the number of lines you'll need to write, it also helps with connection management

combi001