Log In
Sign Up and Get Started Blogging!
JoeUser is completely free to use! By Signing Up on JoeUser, you can create your own blog and participate on the blogs of others!
odyssey
A long wandering and eventful journey
Struts and SWT
Published on May 31, 2005 By
ApKa
In
Tutorials
Well! I am learning Struts and SWT these days.
I tried to integrate Struts with SWT... It's simple! The only problem was that nothing was available on net
or may be I couldn't find anything, and I had to workout things myself.
I am not sure if this is the right place to put a technical document like this or not, but this being my second article, I guess I need some more confidence before I place it on any tech. site.
Prerequisite
: Basic knowledge of Struts and SWT
In this article
This is a very simple example and shows only the basic integration. Here we will create a simple form to take user input for Username, Password and Password Check field.
In next article:
We will verify that the Username is not blank, has more than 5 character and less than 15 characters.
Then we will verify that the password and password check field are same.
After successful validation the user is shown a successful logon page else he is shown an appropriate error.
All of us who have worked with Struts and JSP, this is simple ...but when I had to integrate Struts with SWT, I had to do little bit of brain-storming.
So here we go....
I have created all the files in
strutsTutorial package
Create bean for fields:
Our first step is to create a bean for all the required fields
package strutsTutorial;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;
public class EntryForm extends ValidatorForm {
// Creating our private variables
private String userName;
private String password;
private String passwordChk;
// Getter and setter methods.
public String getUserName() {
return userName;
}
public void setUserName(String string) {
userName = string;
}....................
Create SWT Screen
:
Now we need to create an SWT form for the user input..
This is our SWT Screen.
Create Action
:
........
public class EntryAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
String client = (String) request.getHeader("user-agent");
// Forward control to the success page
if (client.equals("SUCCESS"))
return (mapping.findForward("success"));
else
return (mapping.findForward("failure"));
}
}
Create JSP Files
:
Here we can create 2 JSP files. First one showing that user has successfully registered and another one showing any error during registration.
Errors can be displayed using
html:errors
tag in errors.jsp
Entries Required in Struts-Config.xml
:
First we need to make the form-bean entry:
form-bean name="EntryForm" type="strutsTutorial.EntryForm"
The form-bean name "EntryForm" relates to "strutsTutorial.EntryForm"
Then we need to make the action path entry
action path="/entryScreen" type="strutsTutorial.EntryAction" name="EntryForm"
forward name="success" path="/pages/success.jsp"
This entry specifies that in case of success redirect to success.jsp.
Submit Action
:
In the SWT form we need to specify what should happen when the form is submitted:
submit.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
try {
/ / specify the url.
URL url =
new URL("http://localhost:9080/StrutsProj/entryScreen.do");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("user-agent", "SUCCESS");
// setting the values for bean fields
BufferedWriter out =
new BufferedWriter(
new OutputStreamWriter(conn.getOutputStream()));
out.write(
"userName="
+ userNameText.getText()
+ "&password="
+ passwordText.getText()
+ "&passwordChk="
+ passwordChkText.getText());
out.flush();
out.close();
String c = conn.getHeaderField("Set-Cookie");
BufferedReader in =
new BufferedReader( new InputStreamReader(conn.getInputStream()));
String line;
// printing the jsp page
while ((line = in.readLine()) != null) {
System.out.println(line);
}
The username, password and passwordChk will map to the bean fields.
Once this action is attached to the Submit button, success.jsp page should be printed on the console and the bean values should be set.Since we have not added any validations hence, we are not able to validate the values entered. In my next article I shall cover how to add validations.
Article Tags
tutorials
Popular Articles in this Category
Popular Articles from ApKa
New Job hiccups...
The Zahir
odyssey...........
Comments
1
LifeIs42
on May 31, 2005
not many ppl try mixing struts & SWT, this is something fresh compared to usual struts stuff found on net...i enjoyed reading it...
>>I am not sure if this is the right place to put a technical document like this or not, but this being my second article, I guess I need some more confidence before I place it on any tech. site.
well, its your blog, your own piece of the big wild wild web, put anythin you want, and things like this are a welcome relief from the blog sites with posts about trivial things like ."i sneezed today", " i ate pizza today"....u know what i mean..
2
JavaScout
on Jun 04, 2005
This might be a silly questions but why integrate struts and swt.
Struts is used for processing http requests. It can integrate with a number of engines to create html. But I would not of thought of swt as being a good engine to create html. swt is usally used by gui java programs to manage windows/dialogs.
If you want to create better controls in a browser then JSF might be something to look at instead.
Also some new frameworks can help in writing struts application or even replace struts
The spring framework is pretty good.
Link
3
JavaScout
on Jun 04, 2005
Ok.
I finally see what you are trying to do.
Just a couple of things.
You probably need a few more things in the outputStream to make a valid http request.
Make sure those text fields get url encoded (application/x-www-form-urlencoded) otherwise special characters could cause problems.
Using a true http client would probably be a better way to do this. Jakarta commons has a decent http client
Link
4
LifeIs42
on Jun 10, 2005
When is the sequel to this article coming...?
5
ApKa
on Jun 13, 2005
Thanks for the suggestions:
Make sure those text fields get url encoded (application/x-www-form-urlencoded) otherwise special characters could cause problems.
Yups !!! I have already thought about it. This is a common problem with web-based projects.
Using a true http client would probably be a better way to do this. Jakarta commons has a decent http client
I will surely look into this. Thanks.
6
ApKa
on Jun 13, 2005
The sequel will be here soon... just busy with some other stuff
Welcome Guest! Please take the time to register with us.
There are many great features available to you once you register, including:
Richer content, access to many features that are disabled for guests like commenting on the forums.
Access to a great community, with a massive database of many, many areas of interest.
Access to contests & subscription offers like exclusive emails.
It's simple, and FREE!
Sign Up Now!
Meta
Views
» 8040
Comments
»
6
Category
»
Tutorials
Comment
Recent Article Comments
Gene Hackman Has passed.
Microsoft is Killing Off Sky...
GalCiv IV: Supernova Dev Jou...
Let's start a New Jammin Thr...
LightStar Design - March 202...
Abdi does it again...
LightStar Design - February ...
I'm Getting Another 'New' PC...
Windows File Manager Not Ope...
LightStar Design Windowblind...
Sponsored Links