• Post Reply Bookmark Topic Watch Topic
  • New Topic

How is java code in jsp executed in first request ?  RSS feed

 
nirali shah
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!