wood burning stoves 2.0*
The moose likes Servlets and the fly likes How To Execute Servlet From JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How To Execute Servlet From JSP" Watch "How To Execute Servlet From JSP" New topic
Author

How To Execute Servlet From JSP

Ketan Pote
Greenhorn

Joined: Jun 14, 2012
Posts: 1

Hi, { }

MasterPage.jsp is the main page of my project. and i included following line in MasterPage.jsp

<jsp:include page="Login.jsp"/> This tag add the Login.jsp page into MasterPage.jsp.

But,

Login.jsp page consist a Servlet i.e. Login

When i individually run the Login.jsp file directly from NetBeans IDE then Servlet { Login } executes successfully.

But When,

I run MasterPage.jsp file then it shows Login.jsp Page - - It is ok ! But When i press Login Button of Login.jsp Page then Servlet { Login } Not Executes

Please give me a proper suggestion . . . !



----------------------------------------------------------------------
MasterPage.jsp
----------------------------------------------------------------------
<%@page import="Servlets.Login"%>
<%@page import="javax.servlet.RequestDispatcher"%>
<%@ page contentType="text/html" language="java"%>
<html>
<head>
<title>Master Data</title>
<style type="text/css">
<!--
.Position_Absolute
{
position: fixed;
}
.Button_Shape {
height: 40px;
width: 140px;
}
.style2 {color: #FFFFFF}
-->
</style>
</head>
<body>
<form action="MasterPage" method="post" name="frmMaster" id="frmMaster">
<table width="1295" height="783" border="1" align="center">
<tr>
<td width="153" height="159"><img src="Load.jpg"></td>
<td width="969" bordercolor="#66CCCC" bgcolor="#FFCC66"> </td>
<td width="151" colspan="2"></td>
</tr>
<tr>
<td height="568" bgcolor="#66CC66"><table width="166" border="0" cellpadding="15">
<tr>
<td width="128"><label>
<div align="center">
<input name="RegisterBtn" type="submit" class="Button_Shape" id="RegisterBtn" value="Register">
</div>
</label></td>
</tr>
<tr>
<td><div align="center">
<label>
<input name="ItemBtn" type="submit" class="Button_Shape" id="ItemBtn" value="Item Master">
</label>
</div></td>
</tr>
<tr>
<td><div align="center">

<label>
<input name="SupplierBtn" type="submit" class="Button_Shape" id="SupplierBtn" value="Supplier Master">
</label>
</div></td>
</tr>
<tr>
<td><div align="center">
<label>
<input name="LogoutBtn" type="submit" class="Button_Shape" id="LogoutBtn" value="Logout">
</label>
</div></td>
</tr>

<tr>
<td><div align="center">
<label>
<input name="ListBtn" type="submit" class="Button_Shape" id="ListBtn" value="List Data">
</label>
</div></td>
</tr>

</table></td>

<td colspan="3">
<% if(request.getParameter("LogoutBtn")!=null){
%>
<jsp:include page="Login.jsp"/>
<%}%>

<% if(request.getParameter("RegisterBtn")!=null) { %>
<jsp:include page="MasterCustomer.jsp"/> <%}%>

<% if(request.getParameter("SupplierBtn")!=null) { %>
<jsp:include page="MasterSupplier.jsp"/> <%}%>

<% if(request.getParameter("ListBtn")!=null) { %>
<jsp:include page="DataList.jsp"/> <%}%>
</td>
<tr>
<td colspan="4" bgcolor="#9966CC"><div align="center">Copyright @ 2011-12 { Vidya Praitshthan's Commerce & Science College - Indapur} T.Y.B.C.A. Students { Group No. 14} </div></td>
</tr>
</table>
</form>
</body>
</html>



--------------------------------
Login.jsp
-------------------------------

<%@page language="Java" contentType="text/html" pageEncoding="UTF-8"%>

<html>
<head>
<title>:: Java Servlet ~ Login ::</title>
</head>
<body>

<Center>
<h1> :: Inventory Management System :: </h1><br><br>

<Form Action="Login" Method="POST">

<Table cellpadding="10" border=3>
<tr>
<td colspan="2"><b> <center>:: Authentication Zone :: </center></b></td>
</tr>

<tr>
<td>Username   :  </td>
<td><input type="Text" name="txtUserName" size="20"></td>
</tr>

<tr>
<td>Password   :  </td>
<td><input type="Password" name="txtPassword" size="20"></td>
</tr>

<tr>
<td colspan="2">
<center><b><input type="Submit" name="LoginBtn" value="Login"></b></center>
</td>
</tr>
</Table>
</Form>
</Center>
</body>
</html>

---------------------------------
Login.java { Servlet }
---------------------------------

package Servlets;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import Reuse.*;

class LoginDatabase
{
public Connection cn;
public ResultSet rs;
public Statement stmt;
public PreparedStatement ps;

public LoginDatabase()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbcdbc:IMS");
}
catch(Exception ce)
{
System.out.println(ce.getMessage());
}
}
public boolean doVerifyLogin(String user,String pass)throws SQLException
{
boolean result=false;
stmt=cn.createStatement();
rs=stmt.executeQuery("Select * From UserAccount");
while(rs.next())
{
if(rs.getString(2).equals(user) && rs.getString(3).equals(pass))
{
result=true;
}
}
return result;
}
}
public class Login extends HttpServlet
{
PrintWriter pw;
public void areaSession(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{
resp.setContentType("text/html");
pw=resp.getWriter();
Integer counter=new Integer(0);
String Message;
HttpSession visited=req.getSession(true);

if(visited.isNew())
{
Message="Welcome - "+ req.getParameter("txtUserName") +" You Visiting Us First Time";
}
else
{
Message="Welcome Back - "+ req.getParameter("txtUserName");
Integer oldcounter=(Integer)visited.getAttribute("counter");
if(oldcounter!=null)
{
counter=new Integer(oldcounter.intValue()+1);
}
}
visited.setAttribute("counter", counter);
pw.print("<Center>");
pw.print("<table border='2' caption='Session Report' cellspacing='2' cellpadding='2'>");
pw.print("<tr>");
pw.print("<td>ID</td>");
pw.println("<td>"+visited.getId()+"</td>");
pw.print("</tr>");
pw.print("<tr>");
pw.print("<td>Creation Time :</td>");
pw.println("<td>"+new Time(visited.getCreationTime())+"</td>");
pw.print("</tr>");
pw.print("<tr>");
pw.print("<td>Last Accessed :</td>");
pw.println("<td>"+new Date(visited.getLastAccessedTime())+" "+ new Time(visited.getLastAccessedTime())+"</td>");
pw.print("</tr>");
pw.print("<tr>");
pw.print("<td>You have visited this site :</td>");
pw.println("<td>"+(++counter)+"</td>");
pw.print("</tr>");
pw.println((counter==1)? "time" : " times ");
pw.print("</table>");
pw.print("</Center>");
}
@Override
public void service(HttpServletRequest req,HttpServletResponse resp)throws IOException,ServletException
{
resp.setContentType("text/html");
HttpSession you=req.getSession(true);
pw=resp.getWriter();
pw.print("<html><body>");
try
{
doLogin(req,resp,you);
}
catch(SQLException ex)
{
pw.println(ex.getMessage());
}
pw.print("</body></html>");

try
{
LoginDatabase ld=new LoginDatabase();
ld.cn.close();
ld.rs=null;
ld.stmt=null;
}
catch(SQLException e)
{
e.getMessage();
}
}
private void doLogin(HttpServletRequest req,HttpServletResponse resp,HttpSession you)throws ServletException,IOException,SQLException
{
boolean b;
UserCookie uc=new UserCookie(req,resp);
if(you.getId().equals(uc.readValue("SessionID")))
{
pw.println("<h1><center>You Pressed Back Button From Registration Page . . . !</center></h1>");
areaSession(req, resp);
}
else
{
LoginDatabase db=new LoginDatabase();
b=db.doVerifyLogin(req.getParameter("txtUserName"),req.getParameter("txtPassword"));
if(b==true)
{
pw.println("<br><br><center><h1>Welcome - "+ req.getParameter("txtUserName") +"</h1><br><br>");
areaSession(req, resp);
you.setMaxInactiveInterval(60*20);
you.setAttribute("SessionID", you.getId());
you.setAttribute("UserName",req.getParameter("txtUserName"));

uc.writePair("SessionID",you.getId());
uc.writePair("UserName",req.getParameter("txtUserName"));
pw.print("<jsp:forward page='MasterPage.jsp'/>");
}
else
{
if(!you.getId().equals(uc.readValue("SessionID")))
{
resp.sendRedirect("Login.jsp");
}
else
{
pw.println("<br><br><center><h1> Sorry ! You are not authorized person . . . ! </h1><br><br>");
pw.println(uc.readValue("SessionID"));
pw.println(uc.readValue("UserName"));
}
}
}
}
}


Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

What is the error that you get. Give us some more information. What configurations have you done?


Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
Gaurav Sainii
Greenhorn

Joined: May 18, 2009
Posts: 24
While using masterPage.jsp your MasterPage servlet is being executed. ??
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

nested form is not a valid idiom in html. never use! and i think your outer form getting executed though never use...
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

Seetharaman Venkatasamy wrote:nested form is not a valid idiom in html. never use! and i think your outer form getting executed though never use...


Sharp eyes!! Without code tags, you were able to read those lines.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How To Execute Servlet From JSP
 
Similar Threads
Login JSP if condition and redirecting issue
wrong jar reference
How to to validation in java script involve two jsp page?
how to get utf-8 data from query string in JSP
Doubts in jsp