Creating J2EE application in IntelliJ Idea 6.0 - EJB3/JBOSS +Ajax. Tutorial. Part 2

Now we create web client.

File->New Module-> Web Module->Next

Module name:

Next->Add server descriptor

Press Next until finish.

Double click on web node and you will see General window:

<!--googleAd-->

Press "+" button under Ejb Local References Configured:

Choose LocalLogger-> Name: ejb/logger-> OK

Select JBoss server tab->type JNDI Name: logger

Let's add servlet:

Open web Module settings:

And add module dependency Choose ejb3->

 

Now let's code our Servlet

package sample;

 

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.rmi.PortableRemoteObject;

import javax.ejb.CreateException;

import java.io.IOException;

import java.rmi.RemoteException;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 

ServletOutputStream out = response.getOutputStream();

out.println("<html><body><h1>Hi from servlet</h1>");

process(request,response);

}

 

private void process(HttpServletRequest request,HttpServletResponse response) throws IOException {

InitialContext ic = null;

String s = "id = " + request.getParameter("id") + " command = " + request.getParameter("command") + "\n";

 

try {

ic = new InitialContext();

Object objRef = ic.lookup("java:comp/env/ejb/logger");

 

LocalLogger logger = (LocalLogger) PortableRemoteObject.narrow(objRef, LocalLogger.class);

response.getOutputStream().println( logger.Log(s)+" </body></html>");

 

} catch (NamingException e) {

e.printStackTrace();

} catch (RemoteException e) {

e.printStackTrace();

} catch (CreateException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

 

 

}

 

Don't ask me why print such html in response, because it's just example.

Add to resources New -> index.html

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<script type="text/javascript" src="prototype.js"></script>

<script type="text/javascript">

function sendOnLoad()

{

var pars ="command=someparam&id=someId";

var request = new Ajax.Request(

"ajaxservlet/test",

{

method: 'get',

parameters: pars,

onComplete:somecallback

}

);

 

}

function somecallback(response)

{

$('msg').innerHTML = response.responseText;

}

</script>

 

<title>Ajax page</title>

</head>

<body onload="sendOnLoad()">

<div id="msg"></div>

</body>

 

Now we add Servlet mapping:

Right click on web -> edit -> "+" - > /ajaxservlet/test (be careful here - without slash at the beginning of the string you will get deployment error )

In the next part we will create application project and deploy it's all to server.

 

 

Comments

You can use simple HTML-formatting tags(like <b>, <ul>, <code> and others)