Showing posts with label Servlet. Show all posts
Showing posts with label Servlet. Show all posts

Monday, 22 October 2018

Show / Print a variable value in input type field in Servlet.

Generally in Servlet we use HTML in Java (Java --->HTML), but in above we have to write Java ----> HTML -----> Java. So, we can write this in following manner

A simple Sevlet code can be like -

out.println("<table border='1' cellspacing='3' cellpadding='2'>");

If we want double quote instead of single quote in HTML properties value then code will be -

out.println("<input type=\"hidden\" name=\"place\" value=\"any value\">");

And finally when we want any variable value in value property of input type, then code will be -

out.println("<input type=\"hidden\" name=\"place\" value=\""+ variable +"\">");

Monday, 18 June 2018

Run your JSP page as Servlet.

Developing JSP page is easier than Servlet. But Servlet has some benefit over JSP. So if we want to run Servlet by developing our page in JSP then we can do it very easily. Although we know that all JSP page is first converted into Servlet and then run. But it makes JSP slower than Servlet. Since Servlet is written in Java while JSP is written in HTML, so it becomes difficult to design in Servlet. Another headache with Servlet that it requires compilation after every changes in code.

Servlets are best suited for large data processing and manipulation. It can also be best suited where you don't want reveal your file name without using any platform.

So if we want develop our page in JSP because of easiness in design, custom tags for calling Beans and use of JavaScript and run it as Servlet because of speed and processing then we can do it in following way:-

I assume that you have good idea of running Servlet on Apache Tomcat Server.

Create your JSP page/project on any of your favourite IDE with your desired design and save it to your project root directory. Now run this page on your server. If it is OK then do the following-

1. Go to Tomcat home folder ------> Work --------> Catalina ----------> localhost

Here you will see your project folder. Inside this folder you will find org folder.
In org folder, there is another folder apache and then jsp folder. In this jsp folder you will see the all Servlet  with the name of your JSP page you have created in your project. These Servlets are created when you run your JSP page on browser (as you know that all JSP are first converted into Servlet before run on browser).

2. Copy this folder (org) to your project/WEB-INF/classes location.

Now in your web.xml, do the Servlet configuration like this.

<servlet>
<servlet-name>sm</servlet-name>
      <servlet-class>org.apache.jsp.home1_jsp</servlet-class>
</servlet>

<servlet-mapping>
        <servlet-name>sm</servlet-name>
        <url-pattern>/sm.arv</url-pattern>
 </servlet-mapping>

If your server is capable of running Servlet, it will also run.

Like this you can convert your all pages/project in Servlet.

Advantages:-

This kind of work can also hide your business logic of your coding from others when you have to distribute your project or share with others.

Also you can design your page in JSP and run as Servlet and you don't need to compile it everytime (for compilation just run JSP page automatically it will be compiled).

You can also hide your file extension (.htm, .html, .jsp).


Thursday, 8 September 2016

Session management using user authentication in JSP/Servlet

AMAZON TV & APPLIANCES SALE
Session management is a very crucial technique to check whether authenticated user is using the service or not. Only login process is not enough to manage the authentication. System must be maintained to check every request from the authenticated user. It should be maintained till the user logout.

It means, system should maintain session on every page requested by user. We can see the very strict user authentication in bank websites.

In general, session management should check

  1. Whether user has pressed back button after logout and he/she can view the pages before logout, even he/she has logged out or not?
  2. Whether user can view pages by directly typing the URL in browser's address bar after loged out or not?
  3. Whether user can view pages from browser history or not?
  4. Whether user has pressed browser back button and not pressed back button provided by application, even he/she is logged in (Often in banking applications).

These are some aspects of session management, when we develop any application. This list may be increased.

Now look here, how we manage session using JSP/Servlet.
Create login page.

Login.jsp

<form id="form1" method="post" action="loginvalidate.jsp">
 <table width="835" height="217" border="1" align="center" bordercolor="" bgcolor="#C0EFDE">
        <tr>
          <td width="116" height="40">&nbsp;</td>
          <td colspan="2"><div align="center">
            <h4>Login</h4>
          </div></td>
          <td width="219">&nbsp;</td>
        </tr>
        <tr height="10">
          <td height="56">&nbsp;</td>
          <td width="200"><label>
            <div align="center">Username</div>
          </label></td>
          <td width="272"><label>
            <input name="username" type="text" size="35" style="height:30px;" placeholder="Enter Your Username" required />
          </label></td>
          <td>&nbsp;</td>
        </tr>
<tr height="10">
          <td height="61">&nbsp;</td>
          <td><label>
            <div align="center">Password</div>
          </label></td>
          <td><label>
            <input name="password" type="password" size="35" style="height:30px;" placeholder="Enter Your Password" required />
          </label></td>
          <td>&nbsp;</td>
</tr>
        
        <tr height="10">
          <td height="10">&nbsp;</td>
          <td><label>
            <div align="center">
              <label>              </label>
            </div>
          </label></td>
          <td><label>
            <input type="reset" name="Reset" value="Reset" />
            <input type="submit" name="Submit2" value="Submit" />
          </label></td>
          <td>&nbsp;</td>
        </tr>
        <tr height="10">
          <td height="10">&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
  </table>
</form>

loginvalidate page check user from database and redirect to proper location.

loginvalidate.jsp

<%
       String username=req.getParameter("username");
String password=req.getParameter("password");
       Connection conn = null;
Statement stmt=null;
String user="";
String pass="";
String type="";
try
  {
Class.forName("com.mysql.jdbc.Driver");
conn =       DriverManager.getConnection("jdbc:mysql://localhost:3306/database","username","password");
//db.dbConnect();
stmt = conn.createStatement();
String sql="select username, password, type from users where username='"+username+"' and password='"+password+"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next())
{
user=rs.getString(1);
pass=rs.getString(2);
type=rs.getString(3);
}
if(username.equals(user) && pass.equals(password) )
{
                         HttpSession session = req.getSession(true); 
                         session.setAttribute("user", username); 
                 session.setAttribute("type",type);
                if(type.equals("admin"))
                res.sendRedirect("../IT_JSF/admin/adminpanel.jsp");
                if(type.equals("normal"))
                res.sendRedirect("../IT_JSF/standerd/officehome.jsp");
}
else
{
                res.sendRedirect("../IT_JSF/login-failed.jsp");

}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
                out.println(e);
}
%>


Now after successful logged in by user, check on every page that use is logged in or not by using code

if(session.getAttribute("user")!=null)
{
       //allow to view the page
       //your full page code
}
else
{
     //redirect to login page
}

Done !!!

Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...