• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

How is java code in jsp executed in first request ?

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following is the jsp page, which is rendered when client contacts the server for the page (Usually it should be HTML page). In this jsp page , there is java code which requires info from the client to be filled in the fields and that info is processed on the server. Now when this page is rendered at the first request only, there is no info available from the client as yet. So how will java code in jsp be executed (String userName = request.getParameter("username"); String password = request.getParameter("password");) ? Since there is no object available, will not it throw null pointer exception ? If no, then why not ? where will the values for username and password come from ?

----------------------------------------------------------------------------------------------------

<%@ page language="java" import="java.util.*" %>

<%
////////////// get the login information:
String userName = request.getParameter("username");
String password = request.getParameter("password");

////////////// and save it in the session:

session.setAttribute("username", userName);
%>

<html>
<body>

Welcome, <%=userName%>!
<form action="..." method="get">
....
</form>
</body>
</html>

--------------------------------------------------------------------------------

thanks
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a good example of why proper web app structure is important. And why scriptlets should no longer be used in JSPs.

In any case, yes, there will be no values. But from the code you posted, there will be no NPE because the null values are never dereferenced.
 
nirali shah
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please elaborate more ?

1 without any values in the request, how the java code will be executed ?

2 why will the execution not throw any error ? Is there some default value inbuilt in the values required ?

3 Once the user provides username and password in the html page created because of the earlier request, will the next request again invoke same jsp ?

Please explain with some more details

thanks
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear in mind that while I am answering your questions, this JSP is not an example of good practices.

nirali shah wrote:1 without any values in the request, how the java code will be executed ?

The variables userName and password will get null values. Nothing wrong with that. And it's fine to save null values as a scoped attribute (via setAttribute()). None of those actions will trigger an NPE.

2 why will the execution not throw any error ? Is there some default value inbuilt in the values required ?

NPEs are only thrown when a null is de-referenced. So if you tried to call a String method on the value, then there would be a problem. There is nothing like that in this code.

3 Once the user provides username and password in the html page created because of the earlier request, will the next request again invoke same jsp ?

Impossible to say since your code doesn't show the form action.
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic