Hello,

I have developed a web based application using jsp and connection as Oracle 9i database. The problem encountered is that whenever I am opening any jsp pages, a session is being created in the database which even after being killed did not disappear. In fact it should be that when i login to my site that a session be created but that is not the case. whenever i am opening the site a session is being created even though i did not login. I am sending u the code of one of my jsp page, can u plz verify the codes and let me know if i should add something which does not allow a session to create after opening the site.

Thank you.

********************************************

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<% response.setHeader("Cache-Control","no-cache"); %>
<%@ include file ="dbcon.jsp"%>
<%
String Butt = request.getParameter("sub");
String Logins=request.getParameter("lname232");
String PASS=request.getParameter("lpass232");

String username = "";
String Messa="";
if (Butt !=null){
	String selT=request.getParameter("typeofs");
	int compc=selT.compareTo("Consultancy");
	int compsu=selT.compareTo("Supplier");
	int compse=selT.compareTo("Services");
	int compw=selT.compareTo("Work");
	
	if(compc==0)
	{
	String query = "SELECT * FROM TBL_PPO_USERS where USERNAME ='"+ Logins +"' and PASSWORD='"+ PASS +"'";
	ResultSet resultSet = statement.executeQuery(query);
	if (resultSet.next()){
	username = resultSet.getString("USERNAME");
	session.setAttribute("username",username);
	if (resultSet != null) {
	resultSet.close();
	resultSet = null;
	}
	if (statement != null) {
	statement.close();
	statement = null;
	}
	if (connection != null) {
	connection.close();
	connection = null;
	}
	response.sendRedirect("redoregconsul_Edit.jsp");
	}else{	
	Messa=("Error login/password !");
	}
	}

	if(compsu==0)
	{
	String query = "SELECT * FROM TBL_PPO_SUPPLIER where USERNAME ='"+ Logins +"' and PASSWORD='"+ PASS +"'";
	ResultSet resultSet = statement.executeQuery(query);
	if (resultSet.next()){
	username = resultSet.getString("USERNAME");
	session.setAttribute("username",username);
	if (resultSet != null) {
	resultSet.close();
	resultSet = null;
	}
	if (statement != null) {
	statement.close();
	statement = null;
	}
	if (connection != null) {
	connection.close();
	connection = null;
	}
	response.sendRedirect("redoregsupp_Edit.jsp");
	}else{	
	Messa=("Error login/password !");
	}
	}

	if(compse==0)
	{
	String query = "SELECT * FROM TBL_PPO_SERVICES where USERNAME ='"+ Logins +"' and PASSWORD='"+ PASS +"'";
	ResultSet resultSet = statement.executeQuery(query);
	if (resultSet.next()){
	username = resultSet.getString("USERNAME");
	session.setAttribute("username",username);
	if (resultSet != null) {
	resultSet.close();
	resultSet = null;
	}
	if (statement != null) {
	statement.close();
	statement = null;
	}
	if (connection != null) {
	connection.close();
	connection = null;
	}
	response.sendRedirect("redoregserv_Edit.jsp");
	}else{	
	Messa=("Error login/password !");
	}
	}

	if(compw==0)
	{
	String query = "SELECT * FROM TBL_PPO_WORK where USERNAME ='"+ Logins +"' and PASSWORD='"+ PASS +"'";
	ResultSet resultSet = statement.executeQuery(query);
	if (resultSet.next()){
	username = resultSet.getString("USERNAME");	
	session.setAttribute("username",username);
	if (resultSet != null) {
	resultSet.close();
	resultSet = null;
	}
	if (statement != null) {
	statement.close();
	statement = null;
	}
	if (connection != null) {
	connection.close();
	connection = null;
	}
	response.sendRedirect("redoregwork_edit.jsp");
	}else{	
	Messa=("Error login/password !");
	}
	}
}
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Supplier Login</title>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>

<LINK rel=stylesheet type=text/css href="style/styles.css">
<link href="style/styles1.css" rel="stylesheet" type="text/css" />
</head>

<BODY bgColor=#e9e9e9>
<form method="POST" action="index.jsp">
<TABLE border=0 cellSpacing=0 cellPadding=0 width=780 align=center height="100%">
  <TBODY>
  <TR>
    <TD height=31>
      <TABLE border=0 cellSpacing=0 cellPadding=0 width=779 align=center>
        <TBODY>
        <TR>
          <TD height="31" background="image/2bt1a.gif" class="header"><div align="right">
           <a href="http://ppo.gov.mu">Home</a> | <a href="http://www.gov.mu/portal/site/pposite/menuitem.1aa44665158181fbe8633010a0208a0c/" target="_blank">Contacts 
                  Us</a> &nbsp;&nbsp;&nbsp; </div></TD>
        </TR></TBODY></TABLE></TD></TR>
  <TR>
      <TD height=133><img src="image/2logob.gif" width="227" height="130"><img src="image/2img1b.jpg" width="550" height="130" /></TD>
  </TR>
  <TR>
    <TD height="2"></TD>
  </TR>
  <TR>
    <TD height=8 vAlign=top>
      <TABLE width=779 
      height=24 border=0 align=center cellPadding=0 cellSpacing=0 background="Contact1_files/menubg.gif">
        <TBODY>
        <TR>
              <TD background="image/menubg.gif"><span class="topmenu"><strong><a href="supplierlogin.htm">&nbsp;</a></strong></span></TD>
            </TR></TBODY></TABLE></TD></TR>
  <TR>
    <TD vAlign=top>
      <TABLE border=0 cellSpacing=1 cellPadding=3 width=779 bgColor=#cccccc 
      align=center height="100%">
        <TBODY>
        <TR>
          <TD bgColor=#ffffff vAlign=top>
            <TABLE border=0 cellSpacing=0 cellPadding=20 width="100%" >
              <TBODY>
              <TR>
                <TD vAlign=top align=left><SPAN class=txt1></SPAN>
                  <TABLE border=0 cellSpacing=10 cellPadding=2 width="100%" 
                  align=center>
                    <TBODY>
                    <TR>
                      <TD vAlign=top align=left>
                        <P>  <img src="image/bullet.gif" width="22" height="9"> <span class="title">Accessing Supplier Database</span></P>
                        <div align="center"><%=Messa%></div>
                                <table width="450" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC">
                                  <tr>
                                    <td><table width="450" border="0" align="center" cellpadding="5" cellspacing="0">
                                      <tr >
                                        <td colspan="2" bgcolor="#CCCCCC" ><div align="left" class="logtitle"><img src="image/keys.jpg" width="18" height="30" align="absmiddle"> &nbsp;&nbsp;&nbsp;Supplier Login                                        </div></td>
                                        </tr>
                                      <tr>
                                        <td valign="top" class="text1" >&nbsp;</td>
                                        <td valign="top" class="text1">&nbsp;</td>
                                      </tr>
                                      <tr>
                                        <td width="159" valign="top" class="text1" ><div align="right">User Name  :</div></td>
                                        <td width="320" valign="top" class="text1">
                                          <input type="text" name="lname232" width="200" id="lname232">                                          </td>
                                        </tr>
                                      <tr>
                                        <td valign="top" class="text1"><div align="right">Password:</div></td>
                                        <td valign="top" class="text1"><input type="password" name="lpass232" width="200" id="lpass232"/></td>
                                      </tr>
                                      <tr>
                                        <td valign="top" ><div align="right" class="text1">Type of Service:</div></td>
                                        <td valign="top" ><select name="typeofs" size="1" id="typeofs">
                                          <option value="Consultancy">Consultancy</option>
                                          <option value="Services">Services</option>
                                          <option value="Supplier">Supplier</option>
                                          <option value="Work">Work</option>
                                        </select></td>
                                      </tr>
                                      <tr>
                                        <td valign="top" >&nbsp;</td>
                                        <td valign="top" >
                                          
                                            <div align="right">
                                              <input type="submit" name="sub" value="Sign in" id="sub">
                                            </div></td>
                                      </tr>
                      </table></td>
                                  </tr>
                                </table>
                                <p>&nbsp;</p>
                                <p>&nbsp;</p>                              </TD>
                    </TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR>
  <TR>
    <TD height=8>
      <TABLE border=0 cellSpacing=0 cellPadding=0 width=779 align=center 
      height=8>
        <TBODY>
        <TR>
          <TD vAlign=center align=middle><IMG src="Contact1_files/spacer.gif" 
            width=1 height=1></TD></TR></TBODY></TABLE></TD></TR>
  <TR>
    <TD height=20>
      <TABLE border=0 cellSpacing=0 cellPadding=0 width=779 align=center>
        <TBODY>
        <TR>
              <TD width=620 height="20" background=image/2bt1a.gif><SPAN 
            class=footer>&nbsp;&nbsp;&nbsp;&nbsp;Copyright © Procurement Policy Office  </SPAN></TD>
        </TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<%
	if (statement != null) {
	statement.close();
	statement = null;
	}
	if (statement2 != null) {
	statement2.close();
	statement2 = null;
	}
	if (connection != null) {
	connection.close();
	connection = null;
	}
%>
</form></BODY></HTML>

You need to separate your presentation from business logic. In simple terms JSP should only present what ever data and call upon servlet to do computation like connecting with database. You may want to have look on this post how to approach your task

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.