Maplet and JDBC

By keli_26 on Jul 22, 2010

create a maplet class and get URL parameter and put them in database by using JDBC and sql .maplet is a framework base on MVC. It make project shorter and cleaner. Since you dont need to create a sevlet for each jsp and you can process several jsp request in one maplet. Also it carry the URL from first request to the end of the pageworkflow.
to use this snippet please download shine pattern and add its to your project.

add maplet.tld near your web.xml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
  <tlibversion>1.0</tlibversion>
  <jspversion>1.1</jspversion>
  <shortname>maplet</shortname>
  <tag>
    <name>Service</name>
    <tagclass>org.j2os.shine.maplet.tag.Service</tagclass>
    <bodycontent>JSP</bodycontent>
    <attribute>
      <name>name</name>
    </attribute>
  </tag>
  <tag>
    <name>SecureService</name>
    <tagclass>org.j2os.shine.maplet.tag.SecureService</tagclass>
    <bodycontent>JSP</bodycontent>
    <attribute>
      <name>name</name>
    </attribute>
  </tag>
  <tag>
    <name>ErrorService</name>
    <tagclass>org.j2os.shine.maplet.tag.ErrorService</tagclass>
    <bodycontent>JSP</bodycontent>
  </tag>
</taglib>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

add AddEmail.jsp it is request page
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>untitled</title>
  </head>
  <body>
    <DIV align="center">
      <P>&nbsp;</P>
      <P>
        <STRONG><FONT size="5">Add a new Email to my email list</FONT></STRONG>
      </P>
      <P>&nbsp;</P>
      <P>&nbsp;</P>
      <form action="../../servlet/Model.Controller" method="get">
        <DIV align="left">
          <P>Name: &nbsp;
            <input type="text" name="name"/>
          </P>
          <P>Email: &nbsp;
          <input type="text" name="email"/></P>
          <P>
            <input type="submit" value="Submit"/>
            <input type="reset" value="Reset"/>
          </P>
        </DIV>
      </form>
    </DIV>
  </body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

add SearchEmail.jsp
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>untitled</title>
  </head>
  <body>
    <P>&nbsp;</P>
    <P align="center">
      <STRONG><FONT size="5">Search an email in my list</FONT></STRONG>
    </P>
    <P>&nbsp;</P>
    <form action="../../servlet/Model.Controller" method="get">
      <P>Name: &nbsp;
        <input type="text" name="name"/>
      </P>
      <P>
        <input type="submit" value="Submit"/>
        <input type="reset" value="Reset"/>
      </P>
    </form>
  </body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

add AddPhoneNumber.jsp
================================================
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>untitled</title>
  </head>
  <body>
    <DIV align="center">
      <P>&nbsp;</P>
      <P>
        <STRONG><FONT size="5">Add a new phone to my phone book</FONT></STRONG>
      </P>
      <P>&nbsp;</P>
      <P>&nbsp;</P>
      <form action="../../servlet/Model.Controller" method="get">
        <DIV align="left">
          <P>Name: &nbsp;
            <input type="text" name="name"/>
          </P>
          <P>phone num: &nbsp;
          <input type="text" name="ph_num"/></P>
          <P>
            <input type="submit" value="Submit"/>
            <input type="reset" value="Reset"/>
          </P>
        </DIV>
      </form>
    </DIV>
  </body>
</html>
================================================

add SearchPhoneNumber.jsp
================================================
<%@ page contentType="text/html;charset=windows-1252"%>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>untitled</title>
  </head>
  <body>
    <P>&nbsp;</P>
    <P align="center">
      <STRONG><FONT size="5">Search a&nbsp;phone&nbsp;number in my phone book</FONT></STRONG>
    </P>
    <P>&nbsp;</P>
    <form action="../../servlet/Model.Controller" method="get">
      <P>Name: &nbsp;
        <input type="text" name="name"/>
      </P>
      <P>
        <input type="submit" value="Submit"/>
        <input type="reset" value="Reset"/>
      </P>
    </form>
  </body>
</html>
================================================

add EmailServiceResponses.jsp
================================================
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ taglib uri="/WEB-INF/maplet.tld" prefix="maplet"%>
    <maplet:Service name="add_success">Your email address had entered successfully.<br/>name:<br/><%=request.getParameter("name")%><br/>email:<br/><%=request.getParameter("email")%> </maplet:Service>
    <maplet:Service name="search_success"><%=request.getParameter("name")%>'s email : <%=request.getParameter("email")%></maplet:Service>
    <maplet:Service name="add_error">your email address failed! there was a problem!</maplet:Service>
    <maplet:Service name="search_error">you had entered a wrong value!</maplet:Service>
    <maplet:ErrorService>No Access!</maplet:ErrorService> 
================================================

add PhoneServiceResponses.jsp
================================================
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ taglib uri="/WEB-INF/maplet.tld" prefix="maplet"%>
    <maplet:Service name="add_success">Your phone number had entered successfully.<br/>name:<br/><%=request.getParameter("name")%><br/>phone:<br/><%=request.getParameter("ph_num")%>  </maplet:Service>
    <maplet:Service name="search_success"><%=request.getParameter("name")%>'s phone : <%=request.getParameter("ph_num")%></maplet:Service>
    <maplet:Service name="add_error">your number failed! there was a problem!</maplet:Service>
    <maplet:Service name="search_error">you had entered a wrong value!</maplet:Service>
    <maplet:ErrorService>No Access!</maplet:ErrorService> 
================================================

engine page (maplet)
filename : Controller.java
================================================
package Model;

import org.j2os.shine.maplet.*;
import org.j2os.shine.jconnection.JDBC;
import java.sql.*;

public class Controller extends Maplet {
    public String name;
    public String email;
    public String ph_num;
    private String forward_tag,forward_page;
    JDBC db=new JDBC();

    public void initialize() throws Exception{

        db.login("com.mysql.jdbc.Driver","jdbc:mysql://localhost/j2os", "Username", "pass", true); 

    }

    public void addEmail() throws Exception {
        boolean a=db.executeSQLQuery("insert into email_table (name,email) values ('"+name+"','"+email+"')");
        forward_page="../service/EmailServiceResponses.jsp";
        if (a) {
            forward_tag = "add_success";
        } else {
            forward_tag = "add_error";
        }
    }
    public void searchEmail() throws Exception {
        ResultSet r = db.getSQLQueryContent("select email from email_table where name like '"+name+"'");

        while (r.next()) {
            email= r.getString("email");
        }
        addURL("email",email);
        forward_page="../service/EmailServiceResponses.jsp";
        if (!(email.equals(""))) {
            forward_tag = "search_success";
        } else {
            forward_tag = "search_error";
        }
    }
    public void addPhoneNumber() throws Exception {
        boolean a=db.executeSQLQuery("insert into phon_table (name,phone) values ('"+name+"','"+ph_num+"')");
        forward_page="../service/PhoneServiceResponses.jsp";
        if (a) {
            forward_tag = "add_success";
        } else {
            forward_tag = "add_error";
        }
    }
    public void searchPhoneNumber() throws Exception {
        ResultSet r = db.getSQLQueryContent("select phone from phone_table where name like '"+name+"'");

        while (r.next()) 
        {
            ph_num= r.getString("phone");
        }
        addURL("ph_num",ph_num);
        forward_page="../service/PhoneServiceResponses.jsp";
        if (!(ph_num.equals(""))) 
        {
            forward_tag = "search_success";
        } else {
            forward_tag = "search_error";
        }
    }

    public void rater() throws Exception {
        db.commit();
        forward(forward_page, forward_tag);
    }
}
===================================================

Comments

Sign in to comment.
guest598594   -  Jul 22, 2010

It'd probably be better for you to add this as a script.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.