Thursday, 23 August 2007

Using usebean, servlet, and JSP

The three level above are the components of MVC(Model View Controller), The Model (UseBean) representate Business Logic, The servlet Controll them, and JsP view template.

for example, we have business logic, : (/web-inf/classes/Logic/MyLogic.java)
package Logic;
public class MyLogic{
String name;
public void setname(String name){
this.name=name;
}
public String getname(){
return name;
}
}
---------------
compile
---------------
In Controller , we create (/web-inf/classes/MyController.java)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import Logic.MyLogic;

public class MyController extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
/* instantiate business logic */
MyLogic x=new MyLogic();
x.setname("Abdul Malik Ikhsan");
request.setAttribute("yourname",x.getname());
/* forward to view in jsp page*/
request.getRequestDispatcher("/view.jsp").forward(request,response);
}}
-------------------
compile
-------------------
Let's map your address
-------------------------------
<web-app>
.............

<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>MyController </servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/do</url-pattern>
</servlet-mapping>

.............
</webapp>

In your jsp view (view.jsp)
-----------
Hello ${yourname}
-----------

It's so pretty ?

Wednesday, 22 August 2007

PHP 5, OOP

PHP 5 now support Object Oriented Programming, in this chapter, i will show you an example :
------------------
/* conf.php */
$host="localhost";
$user="root";
$pass="samsonasik";
$db="mydb";
?>
-----------------
/* mysqldb.class.php */

class myqldb{
var $host,$user,$pass,$db,$id;
function connect(){
include "conf.php";
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
return mysql_connect($this->host,$this->user,$this->pass);
}

function getdb(){
include "conf.php";
$this->db=$db;
return mysql_select_db($this->db);
}

function deleteid($id){
return mysql_query("delete from book where id='$id'");
}
}
?>
-----------------------------------
/* deleteid.php */
include "mysqldb.class.php";
$del=new mysqldb();
$del->connect();
$del->getdb();
$del->deleteid("001");
?>

it's so simple ???

Monday, 20 August 2007

Servlet Mapping

We can call "servlet mapping" with the method to manipulate address in address bar in your browser,you can use http://127.0.0.1/bla.bla.bla.do ; etc, which the address is never exist on your server drive. Now what ? we can configure it in web.xml (deployment descriptor).
---------------
For example, we create simple jsp page (/secret/index.jsp)
Code :
Hello World...
-------------------
Now, we create manipulater page with generate servlet code, (/web-inf/classes/learning/show.java)
package learning;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class show extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
req.getRequestDispatcher("/secret/index.jsp").forward(req,resp);
}
}
--------------
compile
--------------
The last, we add servlet mapping in xml code, (/web-inf/web.xml)
-----------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5"><display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description><!--servlet mappingnya--><servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>learning.show</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/go.yes</url-pattern>
</servlet-mapping></web-app>

----------- >> Restart your Tomcat ...
Now, you can call : http://localhost/go.yes
-------------------------------
-------------
(( -- For more about Tomcat Configuration, read Configure Tomcat --))
-------------------------------