Daniel Val

Ranch Hand
+ Follow
since Jan 09, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Daniel Val

Ali Gordon wrote:I now know the basics of JSP and Servlets and I am going to make my own web app (shopping cart). I have some ideas for a simple app in which I can practice
what I have learned. But, I am having great difficulty in making a blue print for my code. But, I don't know if my blue print is useful or even missing something.
Can someone please tell me where I can learn a formal process to make blueprints (not algorithms or flowcharts) for my code/web app ? I want to be able to
make such amazing blueprints, that a beginner coder could simply follow the requirements and do all the coding himself. How do I do this ?

What I did so far -

1 - Finalize a small list of features in the app.
2 - Identify all the entities or classes needed for the app - Customer, Product, ShoppingCart, etc.
3 - A list of all the web-pages/servlets/jsp's that the app might require.

I wonder if I can start coding right away. I hope that I have not missed something which will bite me later and will require a
whole rewrite of the code.

Thanks.



Hi,

Suggestion:
- figure out all the pages you will need.. There will be portions that repeat on each page, like the left or right hand menu, the header, the footer, whatever
- get a good html editor and do all the pages. Do not start coding jsp or anything dynamic till you finished the html
- if you have any ajax or something that will show / hide artifacts using javascript, then your html template must contain all the possible artifacts. Then you hide whatever you have to hide when rendering the page, etc, but you will have all the html you need to work with.

Then start coding. Take out the header and the footer and put jsp:include in your pages. Then get each page and code it. You assume you hold in either request or in the session some value objectd where the rendering information resides. That object, associated with each page, must contain all the info you need to render that page

And a last advice: NEVER use scriptlets in your jsp. Either use custom tags or jstl tags, accessing stuff in that object mentioned above..

Then it is simple: your code will fill out that object, store it in request / session / whatever and then forward to your jsp.
9 years ago

Paul Ngom wrote:Ulf,
Thanks for your reply. But the 200 text fields have to show at a go ready for entry.



I can guarantee that if somebody expects to enter 200 book names and then hit submit, probably the session will expire and the headache will be major. I would suggest you allow entering 10 at a time, and keep adding a number of times.

Otherwise: you generate the page from the servlet, in a for loop you generate the html code with the fields, and that will do the rendering. Personally, considering you have 200 times one field and not one collection of fields, you can give the same name and then retrieve them as an array with request.getParameterValues where you put a name and then you get a String array with all the values.

But again, 200 fields before you submit is an issue and if this is the requirement you have to talk to the business person out of this approach.
9 years ago

stshiva raaj wrote:Hi folks, Can I access the session object even session has been expired? I need to check whether session is expired or not for each request. How to check?
The session invalidation is set null the session object. What I concluded, session time out I can access session object but session invalidation I can not access session object. Am I correct? Then How can I find the
session time out by using session object?



I know what you mean - you login, store something in the session and then you want to look it up and see if it is there. If not, that means the session expired.


Yes, you can do that: just access the object. session.getAttribute("name") etc, and you get null if the object is not there.


I know your concern - other languages / techlologies you need first to check whether the object is there and then retrieve it or else you get a warning. Not here. Just don't try to work with the object if it is null.


D
9 years ago

Tarun Oohri wrote:

Ulf Dittmer wrote:Servlets are not generally thread-safe - they're thread-safe only if you make them so. Not using instance variables can (and should) be part of that, but by itself that is not sufficient.


Thanks Dittmer, please tell me the way i am relating servlet above is right or wrong..Just want to know if i am thinking in right direction or not!!!



Hi,

Although it is possible to have them configured in another way too, most of the people use the servlet configuration where there is only one instance of the servlet which is accessed by a lot of application threads.

The simplest way of making them thread safe: <b>ensure they don't store any state</b>. If you think well, you should not need any state anyway. So no attributes, and you are in the best thread safe situation.

Dan
10 years ago

Supun Lakshan Dissanayake wrote:

Suresh KumarPandey wrote:

...
class Child extends Parent implements Serializable { //pay attention here
int age = 11;



Hi,

You have the Parent class which is not serializable, and then the child class that extends Parent and it is serializable.

I like to approach this sysematically: when I want to serialize an object, I make sure everything is serializable. So why don't you create a base class called BaseData that implements Serializable, and then extend everything from that class. In this way Child, Parent etc will be serializable. I cannot say this is the issue, but I do like that and I never had an issue in the first place.

Here is an article

http://proghowto.com/how-to-use-java-objectoutputstream

Daniel

10 years ago

Luis Oliveira wrote:Hi all

I'm currently starting a project, in my university, which involves studying HTML5 and J2EE integration.

I'm also a Struts user. What is the state of HTML5 integration into Struts?

Is there something implemented or any plans to implement it? Will we see features like Web Sockets, Web Messaging, Web Workers, etc as part of Struts in a near future?

Are there any good sites, articles or other reading material about this subject

Many thanks,
Best Regards



Hi,

html5 reflects purely how an html document is rendered in order to get some behavior from a browser. You can render all the html5 tags using struts any version, as struts is a server technology that will help you render whatever you want. So no matter of the server technology (be it static html, Struts, jsf, php, whatever), you render html5 tags and everything will be fine should your browser support them.

D
11 years ago

Ajay Paswan wrote:Hi,

I am new to this technology, I am using intelliJ IDEA, please give me the pointer to tutorial where from I can learn the simplest web application using struts. It will take a parameter from the client and retrieve any/some information from mySQL and again send this retrieved information to client.



Struts comes with some struts_blank application. Please use that one as a starting point.
11 years ago

Dipali yadav wrote:Hi,
when I click on reset button, it resets only fields but I want that it should reset error messages as well from the page.

Please help me out for this.

thanks...



This is what a <input type="reset"> button does. It will only reset the form fields.

So what you need to do:
- Create a <input type="button" onclick="someFunction" value="Reset"/>
- Define javascript function someFunction that will do the following:

a) reset the error messages
b) document.forms[0].reset(); // this is just like the reset button

how you do b) is clear, how you do a):

- When you render the page, put all your error messages wrapped in some tags that can be easily found in the DOM like
<span class="errorhere">error message</span>

- Then implement a) like that: find all the tags with class errorhere and remove all their children from the DOM. This will remove all the error messages. Don't do it by hand: use a Javascript framework like jQuery.

not sure about the syntax, but something like that:

jQuery('span.errorhere > *').remove();
11 years ago

Vidya Gupta wrote:

Hi,

Can anyone tell me how to use authentication and authorization concept in struts ... please help me..

Regards,
Vidya



If you want to do it yourself, I see two options:

1. you use a filter - if user is not authenticated, you redirect to the login page. The filter should be installed for *.jsp and *.do - this will cover all the dynamic content on the site.
2. you don't use a filter. This is not the best approach but it works. Then you need:
- A custom tag you put in all jsp files that can be accessed only while authenticated: inside the tag, you check if the user is authenticated, if not, redirect to the login page
- Except the action associated to the login page, check in each action method whether the user is authenticated, if not, forward to the login page

Obviously the second method requires more work. Personally I use filters for all the authentication needs.
11 years ago

vishal gaiky wrote:hello friends,

i am developing one online exam application using struts i want to use pagination to show five pages and on each page i want to show 5 questions..now problem is that i am using display tag but i dont know how to iterate it for rows only..i want to show data on rows not on columns like

1. question text.
radio button 1
radio button 2
radio button 3
radio button 4

2. question text.
radio button 1
radio button 2
radio button 3
radio button 4

3. question text.
radio button 1
radio button 2
radio button 3
radio button 4


like above



Probably you iterate something like
<logic:iterate>
<html:radio .... /> <br/>
</logic:iterate>

And that is the reason it starts on the next line. Remove the BR tag and the radio buttons will be displayed horizontally.

If your question was not that but you actually wanted the first question and group shown in a position, then the second one together with its options shown to the right, then you need a layout table. You render a table tag,
then a tr tag, then the first question and options will be in the first td tag, the second and its options in the second td tag etc.

<table>
<tr>
<td>
Question 1 <br/>
radio 1 <br/>
radio 2
</td>
<td>
Question 2 <br/>
radio 1 <br/>
radio 2
</td>
</tr>
</table>

and this will be rendered horizontally, question by question.
11 years ago

Kaur Manpreet wrote:Hi Bear,
My question is regarding the appending of extension .do (or any other specified in <url-pattern>) to the existing URL.

How is this happening?
Is it behind the scenes (as a rule or out of box feature of Struts).

Thanks



Hi,

Struts is an MVC. Like any MVC, it has a front controller, which is the ActionServlet. All the requests within the application must go through the front controller who will then dispatch them to the right action.

The Action Servlet is defined with the url pattern *.do, that's why you see the calls like response.do etc.

How it is done: when you put <html:form /> tag, it will render a html form tag that will have action geared to the *.do url.

If you don't like *.do you can change it, however all the html form tags in your application will be rendered to direct all your posts to that ActionServlet front controller.

D.
11 years ago

Xin Xu wrote:I plan to write a website used by our church in Java, any recommendation for Java application web hosting provider?
...
Anybody any comment or suggestion? Thank you.




Hi,

There are java hosting packages available, however I don't recommend them. Today, if you know how to administrate a linux, when it comes to java you should go ahead with a VPS. One with 512 megs of RAM should get you up and running pretty well. If your application evolves and you require more RAM, you can upgrade the vps.


Please search "vps hosting" on google and see what you get. Regardless of what you take, make sure that you do regular backups of your contents - database especially - as things happen.

D.
11 years ago

Michele Smith wrote:Okay but my application has hundreds of servlets.

Without using the dispatcher method, how can I effectively link this method encapsulated in its own class, to those hundreds of servlets?

Is making it a helper class sufficient to do this? Should my efforts be geared to making those hundreds of servlets use a helper class?

Thank you, and I am sorry if I am really spacy sometimes Bear, you are very helpful and I am grateful for your advice, thanks again,



Create a base class for your servlets and add this method. If you don't want to do that because the method is in some helper, you can either call it from your servlets directly referring the helper, or if you don't like this, still create a servlet base class, a method in that class that will delegate the call to your helper.

D

11 years ago

Viswanathan Pachaiyappan wrote:I have a jsp page wherein i have to passs the file as input and read the contentsn of a file in servlet.

upload.jsp

<FORM METHOD=POST ENCTYPE="multipart/form-data" ACTION="upload"> File to upload: <INPUT TYPE=FILE NAME="upfile">
<INPUT TYPE=SUBMIT VALUE="Submit">

upload.java

//In upload servlet page i have tried to read the contents of a file in doPost method. I have used

String t1=request.getParameter("upfile");

//It shows null. How to read the content in a file and display in servlet?



Hi,

It is more complicated than that. The multipart request has a more convoluted structure and you should not start from scratch with its processing.

Please get the library FileUpload from Apache Jakarta, study how to operate with it and that will provide you a solution for your problem.

There are also web frameworks that provide transparent file processing (like Struts etc), but of course your question was about servlets.

Best Regards,
D
12 years ago

Bear Bibeault wrote:Singletons are generally considered a poor pattern. I'm rather agnostic on it myself.

My main issue is that you seem to be saying "Don't do initialization in init(), do it in service()" and I disagree with that. The lifecycle methods are there for a reason and they should be used appropriately. If you're doing something that doesn't require initialization, then obviously there's no need to override init().



No, from service() I call the singleton(s) which would be initialized on demand (first time they are accessed). It is not quite in service.

In terms of singletons, indeed there are issues and some controversy, but for me they were solution saviours for simple caching etc more than one time, so what can I do...

D
12 years ago