Timothy Schmelter

Greenhorn
+ Follow
since Apr 25, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
3
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 Timothy Schmelter

Howdy all,

With the number of collective years of experience on this forum, I'd imagine that many questions can be answered "from the hip," as it were--without having to actually code or test a solution. But for those intriguing problems where only running the code will do, what's your preferred setup?

For example, I spend most of my time lurking in JSP/Servlet land, so I have Eclipse set up with a local "test" project. I'm setting up a generic servlet, filter, and JSP that I can edit based on the specific configuration in question.

What's your secret recipe?

--T
12 years ago
Hi Sneh,

This is a forum to help provide pointers to learn Java, so you're unlikely to find someone willing to simply review hundreds of lines of source code in multiple files so that they can do your homework for you. To get help here, or on any forum, distill your question to its simplest form, post the minimum amount of code needed to fully express your question, and keep your questions focused. In short, ShowSomeEffort. At a minimum, ask a question. Your post simply said "I need it to work." A worthy goal, to be sure, but hard to define.

Based on the one file I did inspect, you're using a JSP to do *everything*, from database setup to conversion to writing to the new database. This is bad practice; you should be using JSPs for presentation and delegating your business logic (i.e., the tasks of uploading files, converting them, writing to databases, etc) to separate, specialized classes. This allows you to break problems into common parts and reuse solutions in multiple locations, rather than recoding everything from scratch each time.

Best of luck on your project. If you have specific questions, do the work and put together a question here and I'm sure we'll jump on it--this community seems very eager to help out people who want to help themselves.

--Tim
Hi Dinesh,

This isn't really a JSP-specific question. There are a couple of ways of making this work, but by far the easiest is to use a JavaScript framework to make an AJAX call to retrieve the results of your form posting, then use those results to update the value as necessary. In order to update the live page, you'll update the "DOM" of the webpage using JavaScript.

Again, a framework is the way to go here, you don't want to reinvent the wheel. I highly recommend looking at jQuery. It's powerful, fairly simple once you understand the underlying concepts, widely used, and actively developed and maintained.

Happy coding!

--T
Hi Ravi,

Instead of using a forward in your JSP, use a redirect implemented in JSP scriptlet code or, even better, catching that condition in your servlet before it gets to your JSP. Either way you do it, though, you are able to then properly respond with a 404 to the properly-formatted bad request.

Scriptlet or servlet, the code differs mainly in how you obtain a handle to the HttpServletResponse. It is automatically included as part of the JSP context. In a servlet, you declare it as part of your servlet's doGet(HttpServletRequest request, HttpServletResponse response) method.

Servlet example


Implementing the redirect in the servlet also saves you a round trip to the intermediary JSP:


--Tim
12 years ago
Hi Ankit,

I am not sure I completely understand your question. Are you saying:
  • You have a custom tag myTag1
  • myTag1 is supposed to output the results of the custom tag myTag2
  • Instead, myTag1 outputs the literal string <myTags:myTag2/>

  • ?

    If so, check to make sure that the declarations of myTag1 are properly configured to accept "scriptless" body content, not "tagdependent." See these links:
  • https://coderanch.com/t/173754/java-Web-Component-SCWCD/certification/Scriptless-vs-Tag-Dependent
  • https://coderanch.com/t/452499/java-Web-Component-SCWCD/certification/Tags-body-content-element
  • https://coderanch.com/how-to/java/BodyTagContent


  • It sounds like you may have myTag1 configured as tagdependent.

    If not, give us some more details and we can look at it a bit more.

    --Tim
    12 years ago
    JSP

    Dinesh Remje wrote:Hello Timothy Schmelter,

    Thanks for your reply. I will note down your suggestions and will try to improvise my code. But any other notifications can you mention inorder to retain the radio button selected values.



    Hi Dinesh,

    Unfortunately, until you Tell The Details about your specific setup (What servlet container are you using? Any additional frameworks? What is the purpose of the page? How much effort are you interested in putting into configuration and setup?), it's difficult to suggest specific tactics to accomplish your desired goal. For example, Deepakkumar's suggestion is certainly one way of doing it, but if you're using a standard servlet container with a JSP expression language (EL), then the easiest solution would be to use the bean's value to set the "checked" attribute directly. In addition, if you're using a framework like Spring or Struts, you may have access to custom tags that will take care of all of that for you; for example, by allowing you to link a Java bean (model), Java controller, and JSP view to easily handle form inputs. However, some of those solution require a significant amount of work to set up, and if you're just doing a simple one-off registration form, it's probably overkill.

    --Tim
    12 years ago
    JSP
    Hi Dinesh,

    If I read the question correctly, you're asking how this HTML form can maintain state between page reloads, assuming the user has selected a radio button option other than the default checked value of "BE/B.Tech".

    In general, the answer is, "You can't guarantee that will happen." In practice, the behavior of the page depends on the browser in which your user is loading the page. For example, on my system (Mac OS X, Firefox 4), Your HTML snippet loads initially with "BE/B.Tech" selected. I can select a different option, and do a "soft" refresh of the page (CMD-R, or press the "Reload current page" button), and my selection is maintained. However, if I do a "hard" refresh (a Shift-CMD-R), then the browser reloads the page from disk rather than from cache, and "forgets" my previous selection.

    That behavior is different in other browsers; older versions of IE, for example, "forget" the selection upon every page reload.

    If you want the selection to maintain between page refreshes, you will have to persist the state somehow outside of the HTML code itself. The two suggestions that come to mind are:
  • Submit the form to your server and remember the incremental state in the session. When the page is loaded, you can set the "checked" attribute based on the previous value.
  • Code a JavaScript routine to store the value in a session cookie in the browser, and do the same thing with the "checked" attribute.


  • Which one to use depends on your particular requirements, but a good rule of thumb is not to store too much information in a browser cookie.

    As an aside, I've pasted some suggested revisions to your code sample below. Of specific note: the label element should only be associated with a single form control; each radio button qualifies as one control and should therefore be labeled individually. In addition, adding a label element to each radio control allows a user to click the text of the label, rather than only the radio button itself. This is a usability improvement, since it's easier to click a large text label than a little radio button.

    Finally, if the class="epara" attributes on your radio button inputs are used only as CSS selectors for styling, you can remove them in favor of the CSS selector path #qualification input, where #qualification is the element ID of a DOM element that contains the radio buttons you wish to style. Below, the fieldset element fulfills that function.



    Hope this helps.

    --T
    12 years ago
    JSP