Call Toll Free!
   packages   ::    about us   ::   support   ::   contact   ::   network   ::   F.A.Q.
    home

 

Programming in JSP/Servlets (Tomcat)

back to index

    1. Code Examples
    2. Connecting to MySQL
    3. Connecting to PostgreSQL
    4. Making a Servlet
    5. Using JSTL (preferred method)
    6. JSP Programming Links

    1. Code Examples

    2. 
      
      <%
      out.println("Hello World!");
      %>

      You can download and example Java Mail script here (Rename the file to "mail.jsp").

      top

    3. Connecting to MySQL

    4. NOTE: It is preferrable NOT to use the below example, but rather JSTL to accomplish this. Please see this link.

      If you are using MySQL with JSP you will need the Class.forName path to the MySQL Driver:

      
      			
      <% import java.sql.*; Class.forName("com.mysql.jdbc.Driver").newInstance() java.sql.Connection conn; conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database_name?user=your_username&password=your_password"); statement = conn.createStatement(); rs = statement.executeQuery("SELECT * FROM mytable"); while (rs.next()) { out.println(rs.getString("myfield")+"<br />"); } rs.close(); conn.close();
      %>

      top

    5. Connecting to PostgreSQL

    6. NOTE: It is preferrable NOT to use the below example, but rather JSTL to accomplish this. Please see this link.

      If you are using PostgreSQL with JSP you will need the Class.forName path to the PostgreSQL Driver:

      
      			
      <%@ page import="java.sql.*" %>
      <% Connection con = null; Class.forName("org.postgresql.Driver"); con = DriverManager.getConnection("jdbc:postgresql://localhost/YOUR_DATABASE", "USER", "PASSWORD"); Statement stmt = con.createStatement(); String sql; sql = "Select * from YOUR_TABLE"; ResultSet rs = stmt.executeQuery(sql); //rs.absolute(10); while (rs.next()) { out.println(); // add your table fields here .... }
      %>

      top

    7. Making a Servlet


    8. 1) Once logged in to SSH Telnet, make a new, empty file called "MyHelloWorld.java" in your "WEB-INF/classes/" directory (java must already be enabled on your account)
      2) Paste the following text into it:

      
      
      import java.io.*;
      import java.text.*;
      import java.util.*;
      import javax.servlet.*;
      import javax.servlet.http.*;
      
      public class MyHelloWorld extends HttpServlet {
      
          public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
              response.setContentType("text/html");
              PrintWriter out = response.getWriter();
              out.println("<html>");
              out.println("<body>");
              out.println("<head>");
              out.println("<title>Hello World!</title>");
              out.println("</head>");
              out.println("<body>");
              out.println("<h1>Hello World!</h1>");
              out.println("</body>");
              out.println("</html>");
          }
      }
      

      3) make sure you are in the WEB-INF/classes folder

        [user]$ cd /home/YourUserName/html/WEB-INF/classes

      4) then, run these two commands to make sure the path to java and the servlet jar file is found:

        [user]$ export JAVA_HOME=/usr/java
        [user]$ export CLASSPATH=/var/tomcat5/common/lib/servlet.jar

      5) compile the class using the javac command:
        [user]$ javac MyHelloWorld.java

      6) You will now have a file called "MyHelloWorld.class"
      7) Redeploy your JVM (from the control panel) and then point your browswer to your site to execute it ( http://yoursite.com/servlet/MyHelloWorld )

      top

    9. Using JSTL (preferred method)

    10. JSTL is the new preferred method for coding in JSP. We recommend ALL customers use this instead. Please see this link for more details.

      top

    11. JSP Programming Links

    12. here are some excellent sites that have help forums and code examples:
      :: jGuru
      :: Big-Boys JSP
      :: Hot Scripts
      :: Servlet links
      :: Many, many more...

      top

      back to index


©2000-08 PerformanceHosting.net,Inc. All Rights Reserved. Privacy Policy