How to create Session Object?
I am writing a file which will be treated as Servlet or JavaBean in different situation. As a servlet we have the doPost method:
doPost(HttpServletRequest request, HttpServletResponse response)
so that I can get the session object in this way:
HttpSession session = request.getSession();
But in other method , such as mymethod(), how can I create a session object and so that I can get the appropriate session attribute defined in my jsp page?
[474 byte] By [
roamera] at [2007-11-27 11:52:57]

# 1
Hi,
in jsp by using session implicit object is there,
(or)
you can use like this way also
<%@page import="javax.servlet.*" %>
<%
HttpSession hs=request.getSession();
if ( hs.getAttribute( "user" ) == null )
{
//response.sendRedirect ( "./index.jsp" );
}
else
--
%>
<%!
public <returntype> yourMethod ( .... )
{
// code here
}
%>
# 2
thank you for your help.
But my question is in my x.jsp file, I have a tag
<jsp:getProperty name="showuser" property="deleteUser"/>
Which call the java file getDeleteUser() method. In this method, how can I create a session object? As it is not calling the doPost() method, which default I can get use of the request object.
# 4
[nobr]DeleteUser.jsp
<jsp:useBean id="userdata" class="user.UserData" scope="session"/>
<jsp:useBean id="showuser" class="html.ShowUser" scope="session"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script language="Javascript" src="../js/UserControl.js"></script>
<title>Delete User</title>
</head>
<body>
<div align="center">
<h5>Delete User</h5>
x${userdata.username}x
<jsp:getProperty name="showuser" property="deleteUser"/><br/>
<jsp:getProperty name="userdata" property="message"/>
</body>
</div>
</html>
ShowUser.java
public String getDeleteUser() throws Exception{
HttpSession session = request.getSession();
UserData k = (UserData)session.getAttribute("userdata");
Integer count = db.GetRecordCount("SELECT count(*) AS count FROM admin WHERE username='"+k.getUsername()+"' order by id ASC");
}
My purpose is when user access DeleteUser.jsp, it calls the ShowUser object to access getDeleteUser() method, which requires to access a session attribute that is an UserData object.
When this java file is not called as servlet in doPost(), how can I get the Sessoin object?[/nobr]