Thursday, January 9, 2014

To remove a session attribute/ To expire a session in JSP

Suppose you want to remove a session. Like you are creating a Log-Out page for your website and want the user session to expire , for that you need to follow the given code!

<%
            String res=(String)session.getAttribute("username");
            if(res==null)
            {
                out.println("Please Login First!!");
            }
            if(res!=null)
            {
                session.removeAttribute("username");
                out.println("You have successfully logged out!");
                out.println("To continue ...Please login");
            }
 %>

In this example when the user successfully log-ins we create a session Attribute and put his username in the session!
username=rs.getString("firstName");
//Here we get the name of user from database.
session.setAttribute("username",username);
//Put it in the session 

Wednesday, January 8, 2014

To send a value with hyperlink tag in JSP

To send a value with hyperlink tag follow the given code...

<a href="Product.jsp?value=<%=bookname%>"> <%= bookname %></a>

here to send the value bookname to product page I have written the given code. bookname is a String variable.


or simply

<a href="Product.jsp?value=1">Click here to go to Product page</a>

now on the next page(or Product.jsp page) simply write

String val=request.getParameter("value");

To Insert an image using tag in JSP (in NetBeans IDE)

To insert an image in JSP using <img> tag you need to follow the given steps...

  • First put the "images folder" in  "Web Pages" Folder. Place your images in image Folder.
         Web pages folder stores all your web pages which you have created!
  • Now write the code given below...

String ima=rs.getString("CoverImage");        
     //getting the image name from MySql database.(eg hand.JPG)
String image=request.getContextPath()+"/images/"+ima+"";              
                              //ima is the String var 
<img alt="image" src="<%= image %>" height="100" width="100"/>
//place this anywhere you want to display the image

You can also mention the name of your image directly in place of "ima" object.
String image=request.getContextPath()+"/images/photo.JPG";