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

Comments:

nice post. can i ask u some specific question. Seems you might have the answer. i thank you in advance.

Posted by Md. Abdur Rahman on Junho 04, 2008 at 07:38 PM BRT #

Hi Abdur. Thanks.
Feel free to ask anything. Here or by e-mail: Jonas dot Dias at Sun dot com
Cheers

Posted by Jonas on Junho 05, 2008 at 03:03 PM BRT #

Great post man! just what I was looking for. I'm just starting with Java and I have to create a web service to retrieve some data from a database using a web service.

Can I ask you how to connect to a Web Service that is on a remote machine, using its URL or IP address?

Posted by Robertux on Junho 13, 2008 at 08:30 PM BRT #

Hi Robertux,
Thanks for commenting.
You may use the WSDL URL. When you ask for a new Web Service Client on NetBeans, for example, you may specify the project the Web Service is on, but you can give the URL to the Service's WSDL file too.
Right?

Cheers,
Jonas

Posted by Jonas Dias on Junho 17, 2008 at 04:44 PM BRT #

Hie,
I am a beginner to java and this post really helped me to start off. I am facing a problem with this code. When i run my Service, it opens the tester page where i add a user. To check the user, i run the service again and check the user. Everytime it says false. what am i doing wrong? i have literally copied my code from here. :/
I have to complete a project and i am stuck. Kindly help.
Saima

Posted by Saima on Junho 18, 2008 at 09:44 PM BRT #

Hi Saima,

You may be forgetting a little detail. Remember that I said on the post that, for simplicity, I'm using an List to keep the Users' data on the Service, right?
Well, when you start the service and add an user, the data is added to the List and is kept on the server's memory. You said that you run the service again to check the user; When you do that you are probably restarting the Web Service, and therefore, restarting the List (every time the service is restarted, the List will be cleared, naturally). Try Running the Web Service, Test it adding some users and check the user login without running the service again (after adding the user, you may go back on the browser to the test index page and use the checkUser operation).
If you need a system that will not clear the User Data after restarting (thats definitely more interesting), you may use some kind of data base.

If the problem persist, I will send you my NetBeans project instead of the pieces of code.
Hope it helps!
Jonas

Posted by Jonas Dias on Junho 19, 2008 at 03:28 PM BRT #

Very nice article that dive through the details in netbeans 6.

I just got a weird bug when generating the client stubs for a JAX-RPC webwervice, that is the IDE generates the clients stubs in the "build" folder and not in the "scr" folder, i.e the generated classes are complied already (.class) and i don't have the access to the .java files.
Is this normal in netbeans 6, if so, i think it may be a bug or so,
cuz i got compilation errors when trying to invoke the webservice from the java code.

Any help will be much appreciated.

Posted by Ahmed Moharram on Julho 10, 2008 at 12:57 AM BRT #

Very good demo.Its really fantastic.

Posted by Anu VInaya on Julho 22, 2008 at 02:58 AM BRT #

Hello Jonas
Tq. Its one of the gr8 articles. It works well in netbeans IDE. Can you pl. tell how to deploy the webservice developed using NetBeans IDE on another webserver (say Apache Tomcat 5.5) running on local machine .

Posted by Swathi on Agosto 05, 2008 at 08:43 PM BRT #

Hi Swathi,

I will make a new blog post about it! Deploying a simple WS on Tomcat and JBOSS. For Tomcat, you just have to use the JDK version 1.6.0_04 or newer (I'm using the update 7 one).

Cheers

Posted by Jonas Dias on Agosto 07, 2008 at 07:09 AM BRT #

hello Jonas Dias,
i have install NetBeans 6.1 correctly, but my NetBeans 6.1 can not create new web application. that i can't found create new web application in File>>New Project. i just found create java application,mobile,and uml. why ?? please tell me the solution.

Posted by mazhel on Setembro 04, 2008 at 09:59 PM BRT #

Hi Jonas,
When I tried to add a new WebService it is asking me to give a valid package name. When I select Default package, it doesn't take it. May I know the solution. Thanks so much.
Regards,Vivek.

Posted by vivek on Setembro 11, 2008 at 08:21 PM BRT #

i want to used servlet code using netbeans IDE 6.1 n apache tomcat5.5 but i dnt know how to write. only name and password i just want to show up

Posted by rameshori on Setembro 24, 2008 at 10:07 PM BRT #

hello, can u give me a guide to develop a system using JSP.. n The system interact with database... n i don't know which database application should i use. plz help me. i'm new in this language, so need help...

Posted by anaRodhi on Outubro 21, 2008 at 11:07 AM BRST #

Hi Jonas!
Great post!, congratulations!, please: I need to create users to access Oracle XE Database, all users with different % of accessibility (system accessibility). Do you have any tutorial or can you help me or give me some ideas how can I do it? I am new in Java.
Thank you in advance.

Posted by Tito on Fevereiro 13, 2009 at 11:31 AM BRST #

Hi Jonas.
Thanks for your post. Please let me know why I should RESTART my GLASSFISH(v2) server to test the web services. Is there any configuration? If yes where should be changed?
The last but not least is the Netbeans(6.5) speed. It is very very slow when I want to deploy and test web sevices. Should I set any system variables. (My computer has about 3GB RAM)
Apritiate if you answer these questons.

Posted by oddfans on Março 10, 2009 at 06:00 PM BRT #

hi Jonas!

Great post!, please: I need to create users to access Oracle XE Database, all users with different % of accessibility (system accessibility). Do you have any tutorial or can you help me or give me some ideas how can I do it?

Thank you in advance

Posted by diana Rabai on Março 22, 2009 at 07:18 PM BRT #

Greetings,
I very much thank you Jona, for providing this tutorial. It is very easy to understand especially for someone new to JSP like me (of course, not entirely new to web programming and Java). But some details were missing, like setting the procedure setLogin and setPassword. It should be easy to figure though. Currently I am learning to build a web-based instant messenger with JSP/AJAX. This tutorial helped a lot. Thanks again!

Posted by Ayyub Mustofa on Abril 30, 2009 at 02:37 AM BRT #

Hi Jonas, this is an awesome article. Will email you on my specific question. Thanks in advance and keep up with the good work!

Posted by Mwamba Chishimba on Novembro 08, 2009 at 09:18 AM BRST #

this is cool, this is what we want dude......

Posted by links london on Novembro 26, 2009 at 04:27 PM BRST #

Hi Jonas,

Its one of the gr8 articles. It works well in netbeans IDE. Can you pl. tell how to deploy the webservice developed using NetBeans IDE on another webserver (say Apache Tomcat 5.5) running on local. machine .

thanks in advance,.

Posted by amit on Dezembro 21, 2009 at 02:53 AM BRST #

Hi,

Thank you very much for ur Example application.Its awesome and easily understandable.

I have one doubt like ,
After adding a user from the webservice and checking the same..
and if i try running index.jsp after entering the login credentials and on clicking submit ...
i always get result as false(Access Denied)

Posted by Gopal on Dezembro 30, 2009 at 12:33 AM BRST #

Thank you for the sensible critique. Me & my neighbour were preparing to do some research about that. We got a good book on that matter from our local library and most books where not as influensive as your information. I am very glad to see such information which I was searching for a long time.This made very glad Smile

Posted by Abercrombie sale on Janeiro 09, 2010 at 04:00 PM BRST #

Thank you for the great article.It is a good one specially for begginers like me.
Wel i wanted a piece of information from you.
I'm creating a Contact Management Web service where i have dynamically to accept the credentials similar to uname and password here.Can you please how i can go about it.

Thank you in advance

Posted by Vanitha on Fevereiro 04, 2010 at 04:49 AM BRST #

Hi Jason,

That's a great tutorial. Thanks so much. Being a beginner to J2EE, I had a tough time reading about all the 'deep' details (WEB-INF, web.xml, etc). Your tutorial using Netbeans really helped me grasp the basics. I did everything as you said till "Now, we may just Run our Web Service, and see that it's Running on the Service tab under your container's Web Applications."

I right clicked on the project folder and clicked Run. And, I got my first 404! What could have gone wrong ?

Posted by Sagar Hatekar on Fevereiro 20, 2010 at 11:57 AM BRST #

Hello,

I m getting an "Access Denied" below "Hello World" string when running index.jsp. Can someone tell me what I ve done wrong...

Posted by N4N on Julho 18, 2010 at 09:57 AM BRT #

Hi, thanks for this great tutorial!!
Can you provide the full project source code?Thanks

Posted by firestruck on Agosto 16, 2010 at 04:09 AM BRT #

Thanks a lot Jonas.
This was very helpful for me starting up with jsp.

Posted by yogesh on Setembro 26, 2010 at 03:48 AM BRT #

Thanks so much for this easy to follow tutorial! I realize I'm writing this nearly 3 years after you wrote it, but it saved me so much time and frustration while creating a WS Client in Netbeans that I had to say thanks!

Posted by bcrowell on Março 05, 2011 at 08:49 AM BRT #

Post a Comment:
  • HTML Syntax: NOT allowed
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