I doing some project based on authentication using servlet/jsp.When user log in initially using username and password where authentication takes place through login servlet,there I need to save user's email in variable ,say String email by executing SELECT query.
I need to access that variable from login servlet to email servlet for sending some sort of OTP to user's email.
How to achieve that using session attribute or any relevant idea??
Use session.setAttribute() and session.getAttribute() methods.
Read javadoc of HttpSession here.
You can refer this complete example.
To save data in session you should use session object from http request like this:
HttpSession session = request.getSession();
session.setAttribute("email", email);
To retrieve data from session object with scriptlet use:
<%= session.getAttribute("email")%>
or
<%= request.getSession().getAttribute("email")%>
You can also use EL expression:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:out value="${sessionScope.email}"/>
please use as follows. you can achieve what you need.
<%session.setAttribute( "email", "test#gmail.com" );%>
<%= session.getAttribute( "email" ) %>
The other way that we use.
<c:set var="email" value="test#gmail.com" scope="session"/>
you get this using JS:
var mail ="${email}";
Related
I am executing the following loop to view all my session attributes:
<%
for (Enumeration e = session.getAttributeNames(); e.hasMoreElements(); ) {
String attribName = (String) e.nextElement();
Object attribValue = session.getAttribute(attribName);
%>
<BR>
<%= attribName %> - <%= attribValue %>
Which will output:
user.principal.key / user.ip.address.key / session.user.key
However, I wish to access more of the user information that might not exist in session yet, like other variables I set in the user.java file.
How would be the best way to store these variables in the session, so I may go about printing them to my page?
Set them as a session attributes:
public void doGet(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
session.setAttribute("myVariable", variableValue);
}
Use some tag library like JSTL to access attributes sent from servlet instead of using scriptlets.
In case of JSTL use out to print value of your variable:
<c:out value="${myVariable}" />
Don't forget to include JSTL tag lib in your JSP:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
and to download JSTL dependency from here
I'm a beginner to JSP (started today) and wanted to send some data (which I have got in a JSP file from an HTML form) to a JAVA program where I could use it.
Peterclass is the name of the Java program I'm gonna be making/using.
Suppose my JSP file is like this:
<%# page import="com.example.Peterclass"%>
<%# page import="java.util.*"%>
<html>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
%>
<h1>
Hello, <%=username%>!
</h1>
</body>
</html>
and I wanted to receive the values of username and password variables in a JAVA program (I'm using Tomcat server for the purpose). How shall I do it? Please be as simple as you can be.
Thank you.
First of all, you will need a so called "Servlet" to work with request and response Parameter.
Here is a first tutorial on how to create and use servlets:
Click here
Another tip for you:
It is not state of the art to use so called Scriptlets.
Scriptlets is a form of Java-Code in JSP Sites:
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
%>
You can easily get the parameter you set in your Servlet with:
${requestScope.<<PARAMETERNAME>>}
e.g. ${requestScope.username}
or if your parameter is in the HTTPSession:
${sessionScope.<<PARAMETERNAME>>}
your jsp side would look like this afterwards:
<html>
<body>
<h1>
Hello, ${requestScope.username}!
</h1>
</body>
So, to come back to your question:
In your Servlet you can set the Parameter with
request.setParameter(<<PARAMETERNAME>>,<<VALUE>>)
In this Servlet you can call normal Java-Functions. First step for you is to read through tutorials :)
I have the working Java code here to display a user's username upon login:
<%= "Welcome " + session.getAttribute("username")%>
If a user is not logged in and at my index.jsp page, it will display "Welcome null".
However, I want to display "Welcome Guest" instead.
Is there any way to do this?
You can do that with JSTL:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Welcome, <c:out value="${sessionScope.username}" default="guest" />
Use JSTL tag libraries. JSP scriplets are deprecated almost a decade back.
This is how you do it using JSTL:
Welcome, <c:out value = "${sessionScope.username == null ? 'Guest' : sessionScope.username}" />
See also:
Our JSTL tag Wiki.
How to avoid Java code in JSP files?
when a user logins to my application, he submits a form to be processed through the Servlet. The servlet creates a session for the user. How would I create a link so the user can logout? I cannot seem to directly link to a Servlet. How would I delete the session and link back to the homepage?
Here is a way that I could do it, but it doesn't seem "right".
I could link back to the index.jsp?logout=true. My index.jsp will see if logout is true and delete the sessions.
Is there another way to do it?
Write a servlet mapped to /logout which then executes something like this in doGet:
HttpSession session = request.getSession(false);
if(session != null)
session.invalidate();
request.getRequestDispatcher("/index.jsp").forward(request,response);
It wont matter if the user has a session or not, they will ultimately be redirected to index.jsp.
I found it easiest to do this:
<form method="link" action="logout.jsp">
<input type="submit" value="Logout"/>
</form>
without logout.jsp having this:
<%
session.invalidate();
response.sendRedirect("startpage.html");
%>
Simplest way to do that is make a link of logout like this..
LogOut
And in the "logout.jsp" write below code
<%
session.invalidate();
response.sendRedirect("index.jsp");
%>
logout is not too serious. you can have a simple /logout.jsp just to end session.
Based on cdietschrun's answer, I've made it even more compact:
<%
session.invalidate();
response.sendRedirect(request.getContextPath());
%>
I currently have a JSP page with a Form for the user to enter their name, but what I want is to get the user forwarded to a different JSP page after form submission and to carry on their name to be used.
I don't want to use JSTL EL just simple JSP uses.
I was thinking of using a bean storing the detail in a session but how would it work.
Thanks.
You'd have JSP enter the info into a form and POST it to a servlet. The servlet would validate the form input, instantiate the bean, add it to session, and redirect the response to the second JSP to display.
You need a servlet in-between. JSPs using JSTL are for display; using the servlet this way is called MVC 2. Another way to think of it is the front controller pattern, where a single servlet handles all mapped requests and simply routes them to controllers/handlers.
duffymo you have the best idea but here is a quick solution by just passing things through the JSP.
Here is the JSP with the form
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Simple jsp page</title></head>
<body><form name="test" action="./stackTest2.jsp" method="POST">
Text Field<input type="text" name="textField">
<input type="submit">
</form> </body>
</html>
and then the second page looks like this:
<html>
<head><title>Simple jsp page</title></head>
<body><%=request.getParameter("textField")%></body>
</html>
And then put information in a hidden field, you can get information by using the request.getParameter method. This just prints out what was in the form, but using the same idea for inputting it in to the hidden field in a form. I recommend this as all my experience with sessions have ended in a failure. I STRONGLY DO NOT Recommend this method, MVC is a much better way of developing things.
Dean