Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, August 1, 2007

"Hello, world!" with DWR tool

DWR is tool for enabling RMI on web-server from JavaScript at the client.

What need you do to create first DWR application.

1) Download DWR library.
2) Add description to web.xml

<servlet>
  <servlet-name>dwr-invoker</servlet-name>
  <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>dwr-invoker</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

3) Create a class for remote invocation. Let it be following...
package my;
import ...
public class Server {

    private int count = 0;

    public int getCount() {return count;}

    public void increase() {count++}
}

4) Create dwr.xml description at WEB-INF.
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
"http://www.getahead.ltd.uk/dwr/dwr10.dtd">

<dwr>
<allow>
<create creator="new" javascript="server">
<param name="class" value="my.Server"/>
</create>
</allow>
</dwr>

This means, if your-web-app/dwr/client.js is requested, DWR will create an instance of my.Server class and a JavaScript stub for it. You may see how this stub looks by link your-wed-app/dwr/interface/server

// Provide a default path to dwr.engine
if (dwr == null) var dwr = {};
if (dwr.engine == null) dwr.engine = {};
if (DWREngine == null) var DWREngine = dwr.engine;

if (server == null) var server = {};
server._path = '/jmxWebApp/dwr';
server.getCount = function(callback) {
dwr.engine._execute(client._path, 'client', 'invoke', callback);
}
server.increase = function(callback) {
dwr.engine._execute(client._path, 'client', 'setAttribute', callback);
}

5) Create page using Server invocaion.


<html>
<head>
    <title>DWR_JMX test</title>
    <script type="text/javascript" src='dwr/engine.js'></script>
    <script type="text/javascript" src='dwr/util.js'></script>
    <script type="text/javascript" src='dwr/server.js'></script>
</head>
<body>
    <script type="text/javascript">
        server.getCount(new Function("data", "alert(data);"));
        server.increase(new Function(alert("Value increased"));
        server.getCount(new Function("data", "alert(data);"));
    </script>
</body>
</html>

Tuesday, July 31, 2007

"Hello, world" with Spring MVC

Recently I've started using of Srping MVC for my web apps. So what you need to get started with spring MVC:

1) Download Spring framework and put spring.jar to web server lib path.
2) Add DispathServlet mapping to web.xml
<servlet>
  <servlet-name><strong>spring</strong></servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/index.jsp</url-pattern>
</servlet-mapping>


3) We have called servler instance as spring, so we should create a spring-servlet.xml at WEB-INF.


<xml>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

      <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
              <property name="mappings">
                     <props>
                             <prop key="/*.jsp">dispatchController</prop>
                     </props>
             </property>
     </bean>

    <bean id="dispatchController" class="my.SimpleController">

      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix">
                       <value>/WEB-INF/jsp/</value>
             </property>
             <property name="suffix">
                      <value>.jsp</value>
             </property>
     </bean>

</beans>
</xml>

We described 3 beans here.
urlMaping - is a mapping bean. It should not be registered manually. Spring will automatically parse it during initialization.
dispatchController - is a bean, which will be dispath all requests of *.jsp pages. We write class="my.SimpleController", so we need to create class SimpleController later.
viewResolver - is a bean? which will add prefix and suffix to views for creating valid request.

4) Create class my.SimpleController
package my;

import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SimpleController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("index");
        return mav;
    }
}

This class will dispath all requests to the "index" view. Then "index" view will be changed to "/WEB-INF/jsp/index.jsp" by viewResolver bean.

5) Create "/WEB-INF/jsp/index.jsp".
<html>
  <body>
     "Hello, world!" with Spring MVC.
  </body>
</html>


That's all.

Tuesday, July 24, 2007

How to start RMIRegistry programmatically

I often face with the necessity of programmatical RMI registry starting. So, this is a solving:

try {
    java.rmi.registry.LocateRegistry.createRegistry(1099);
    System.out.println("RMI registry ready.");
} catch (Exception e) {
    System.out.println("Exception starting RMI registry:");
    e.printStackTrace();
}