Quarta-feira Jun 04, 2008

WebServices and JSP on NetBeans 6.1

Turn The Very Easy mode On and Play

Hi Everybody! Today I'm writing a little review (that may sounds like a tutorial) about the easiest way to write a simple Web Service and use it on your projects. See, you don't need to be an expert on Web Services nor JSP nor Web Technologies, because we are using NetBeans 6.1, the only IDE you need to learn something big on the very easy way.

From 6.0 to 6.1, NetBeans has grown faster, more stable and more practical. I haven't found anything specific about the development of WS using the New NetBeans, only using the 5.5 version. That's why I decided to write it here.

I've downloaded the Web & JavaEE Pack of Netbeans 6.1 on NetBeans Download Center. It comes with TomCat and Glassfish bundled, but we can use others Containers too, like JBOSS, for example. On the Services tab, on the left side of IDE, you can find the servers that NetBeans is using.

You may click on 'Servers' and choose 'Add Server...', A list of 'Connectible' servers appears. You will be able to connect Servers like JBOSS, WebSphere, BEA WebLogic, Sailfin, and, of course, TomCat, Sun Application Server and Glassfish, all out-of-the-box. (They just need to be installed, TomCat and Glassfish can be installed during NetBeans Setup).

Now Let's Start our Web Service. We just need to go to File -> New Project (Ctrl+Shift+N), choose Web on Categories and Web Application on Projects. Then Press Next. Choose the name and folders of your application and press Next again, then choose the server where your Web Application will be deployed. You will see the screens below (but they will be shown separated, through the next steps, not layered). On the End, just press Finish. (If you press Next, you will be able to choose a framework like JSF or Struts)

Now we have our Web Project. A Default index.jsp is there waiting - delete it - we are doing WebServices for now. Select the Project, click on the Project name with your right mouse button, choose New -> Web Service... on the list. NetBeans will then open a tab on the development area showing it's graphical interface to create Web Services, just like that:

A Web Service is quite like a java class (it's a .java file), in which the methods are called Operations. A Web Service may have many Operations. So, let's create our Operations clicking at the 'Add Operation...' Button. A window will pop up, asking the details of your operation, just like this:

I created two operations, one to add a user (the addUser Operation show above) and one to check if the user has been added to the list, like a login check (the checkUser Operation). NetBeans shows me the details graphically:

(If the image is too little, click with your right mouse button and choose View Image, The blog may be resizing the pictures, making them unreadable)

Now we go to the Source Code (Clicking on the Source button on the development area, just below the file tab)! You can click with the right button on the Operation and choose 'Go to Source' option on the Menu.
At The Source you see all the Structure of your code. Out of the methods scope, create a List of Users like:
>> List users; . There is no User Class defined in the package, so click on the light bulb near the code and ask NetBeans to create User Class in the package for you:

Go to User Class source code and add two parameters to the class: String username; and String password;
Then we ask NetBeans to create the getter and setter methods, and to override equals and hash code methods. How?? Easy! Just click with the right button and select Insert Code: A little menu will appear:

and we may choose to generate many kinds of methods. For example, if we choose to generate equals and hash code, another windows will pop up asking about which parameters of the class do you want to use, just like this:

Any doubts about the User Class, see it's source here.

Back to the Web Service source code, now we have to implement the addUser and checkUser Operations. They are both easy, see the idea of the code below:
To add a user:
   User user = new User();
   user.setLogin(login);
   user.setPassword(password);
   if(!users.contains(user)) { //if the user is not already on the list
     users.add(user); // add the user and return true to say everything has gone OK
     return true;
   } else { // user is already on the list.
     return false; // Return false to say no one has been added to the list
   }

To check the User:
   User user = new User();
   user.setLogin(login);
   user.setPassword(password);
   if(users.contains(user)) { //if the user is on the list
     return true; // let it login, return OK
   } else { // user isn't on the list
     return false; // return 'there is a problem here'
   }

The Full Web Service Code can be viewed (and copied ;) ) here.

Now, we may just Run our Web Service, and see that it's Running on the Service tab under your container's Web Applications.

But, let's Test it to see if it's really working!
Expand the Web Service Folder of Your Project and select your Web Service. Click with the right button and select Test Web Service:

Something like that will be opened on your Browser:

Amazing, isn't it?? You may test your Web Service as you want, and get answers like this:

Tip: Add some users using the addUser Operation and see if checkUser works returning True for added Users and False for unknown users.

Now Lets quickly create our JSP! Yes... really fast.

Create a New Project (File -> New Project). Select Web / Web Application Again!
Now we may use the 'index.jsp' file. There is a palette on the right side of the IDE, you may drag and drop HTML Form Items to your code. You need a FORM that collect user's login and password. Something like this:

<form name="loginForm" action="checkLogin.jsp" method="POST">
<input type="text" name="login" value="Your Login" />
<input type="password" name="pwd" value="\*\*\*\*\*\*\*" />
<input type="submit" value="Submit" name="loginSubmit" />
</form>

The Form action is checkLogin.jsp... so Let's create it. Click on your new Web Project name with the right button, go to New -> JSP. Create the JSP named checkLogin.
Now let's add a client to our WebService, the one we created on the other Project.
Click with right button on your New Project Name, go to New -> Web Service Client...
Select the Project where the WebService has been Created, just like this:

There are other ways to get the Web Service (thought WSDL link, for example), you may explore it yourself latter.
Wait while NetBeans process the Web Service into your New Project, you will be able to see it on the project. Like this:

(On the properties of the webservice, you will find the WSDL address of the service, then you can visualize it o the browser, and see it's XSD too.)

Now, go to the JSP code. Below the Hello World default Message, click with the right button and select:
Web Service Client Resources -> Call Web Service Operation
A window will appear asking which operation you want to choose:

Choose checkUser and NetBeans will do all the code for you. Of course, you need to put some by yourself. Our FORM, on index.jsp, send it's data using POST. so let's get these values from POST. Add the fallowing code before the Web Service process, just after the 'try {' put by NetBeans on your JSP:
    String uname = request.getParameter("login");
    String pwd = request.getParameter("pwd");

Then, change the generated code lines to point to your collected data:
    java.lang.String login = uname;
    java.lang.String password = pwd;
    // TODO process result here
    boolean result = port.checkUser(login, password);
    if(result) {
       out.println("User Logged");
    } else {
       out.println("Access Denied");
    }
    } catch (Exception ex) {
       out.println(ex.getMessage());
       out.println("Unable to operate the WebService");
    }

Any doubts, check index.jsp and checkLogin.jsp codes.

Then, ask NetBeans to Run the index.jsp file. The browser will be opened with your JSP page asking a login and password (Just after the Hello World! message). Try the login and password you have added using the Test tool and see if checkLogin grants access to the users or not. Try the users you have added and another ones. If no User is recognized, the data on the Server may has been lost, try adding new login/passwords using the Test Tool or using the addUser Operation, now you know how to do it.

You don't need to use a Web Interface to access your Web Service, you can create a Java Project with a GUI that connects to the server and use the Web Service, or use a Mobile, everything through NetBeans. Just Explore!

Remember!! We are keeping this user information on a List inside the Memory (through the Web Service inside the Server). If you restart the Server or the IDE, all information may be lost. This example is just a simple one, to do a more professional system, you should use a database, this way your data will be kept safe.

Hope you enjoy that! The text is quite big, but it's simple to do that!
Any doubts or tips, just send a comment! ;)

Cheers
Jonas

About

Well... Let's keep this blog updated for now on!!

Search

Categories
Archives
« Abril 2014
SegTerQuaQuiSexSábDom
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today