Tazzmission

Greenhorn
+ Follow
since Jan 16, 2002
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tazzmission

I have downloaded a connection pool java in coreservlet, in servlets, I should declare it as public or private? Thanks
public class ServletOne extends HttpServlet
{
private ConnectionPool connectionPool;

public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, java.io.IOException
{ ........
20 years ago
I've created 2 servlet java:
Userlogin.java(code below) -> create a session and user object (using UserLog.java), display "logined in" if user object found.
UserLog.java (code below) ->user object storing simple username.
I placed them in one directory called hello. Works fine. I copied Userlogin.java to s1.java and try to run s1.java servlet after I run Userlogin (which created a session and user object for me), then I run s1.java servlet, it SHOULD display "I've loginned" (because Userlogin.java created a session already), YES it WORKS fine.
HOWEVER, if the s1.java servlet is NOT in the same directory (in here 'hello') and run in root, or other place, it CAN'T work AND just display Internal Server error. Please tell me if I am really CAN'T DO like this? Please tell me the reason why? THANKSX10000

package hello;
import javax.servlet.*;
import javax.servlet.http.*;
public class Userlogin extends HttpServlet
{
// The session constant for the user key
String USER = "USER";
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, java.io.IOException
{
resp.setContentType("text/html");

java.io.PrintWriter out =
new java.io.PrintWriter(resp.getOutputStream());

HttpSession session = req.getSession(true);
// We should always get a session back
if (session == null) {
out.println("ERROR: Internal servlet problem - no session");
out.flush();
out.close();
return;
}
// Get the current user info. If we get back a null, the
// user is not currently logged in
UserLog ulog = (UserLog) session.getAttribute(USER);
// Get the requesting URI - that should be us
// Get our URI
String uri = req.getRequestURI();



try {
// Figure out what was requested

if (ulog == null) {
String user = "myname";
int no=1;
ulog = new UserLog();
ulog.setUser(user);
ulog.setLoginCount(no);
String a=ulog.getUser();
int b=ulog.getLoginCount();
session.setAttribute(USER, ulog);
out.println("<html>");
out.println("create user session success");
out.println(a);
out.println(b);
out.println("</html>");
}
else {
String a=ulog.getUser();
int b=ulog.getLoginCount();
out.println("<html>");
out.println("logined already");
out.println(a);
out.println(b);
out.println("</html>");
}
}
catch (Exception ex) {
// Catch any exceptions and send the stack trace back
// to the client
ex.printStackTrace(out);
}
// Set the response header to force the browser to
// load the html from the Web instead of it's cache
resp.setHeader("Expires", "Tues, 01 Jan 1980 00:00:00 GMT");

// Wrap up
out.flush();
out.close();
}

public void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, java.io.IOException
{
// Same as get
doGet(req, resp);
}
public void init(ServletConfig cfg)
throws ServletException
{
super.init(cfg);
}
public void destroy()
{
super.destroy();
}

}

--
package hello;
public class UserLog
{
String m_user; // The user ID
int m_loginCount; // Number of times that the user has logged in
/**
* <p>Sets the user ID
*
* @param user User ID
*/
public void setUser(String value)
{
m_user = value;
}
/**
* <p>Gets the user ID
*
* @return User ID
*/
public String getUser()
{
return m_user;
}
/**
* <p>Sets the login count
*
* @param count Login count
*/
public void setLoginCount(int value)
{
m_loginCount = value;
}
/**
* <p>Gets the login count
*
* @return Login count
*/
public int getLoginCount()
{
return m_loginCount;
}
}
22 years ago
Hi, I've 2 java
a.java which will user b.java
in a.java and b.java, I've package hello; in first line,
and I've saved a.java and b.java to e:\tomcat\webapps\examples\web-inf\classes\hello
inside hello directory, I compile by using "javac a.java" but it failed saying it can't find b.java,
however if I say a.java and b.java in classes\ , it have no problem in compiling.
Pls tell me how to compile the two java using in \hello\?
Thanks
22 years ago
Hi,
thanks, where can have more examples about filtering on login topic?
Also, before Servlet 2.3 API, that should be many web site dealing this kind of problem (I think it's not a problem to them) I wonder what approach they are using? Without Servlet 2.3API or even Servlet, they may use PHP, etc. But it's not a problem to them, we just don't know what approaches or technique they are using.
22 years ago
no ,what i mean login servlet/user object AND other functional servlet should be in different directory locially. However, im not success in calling back the same object through Servlet in another directories. Say, login.java (create/check session), userLog.java(user Object:username,password,getUsername,getPasswd), chatroom.java, etc.java in the same directory. chatroom.java will check the session object firstly, no problem. But if i move chatroom to another directory, when checking the session object it will be error.

Another thing is, this technique is also bulky for course material, if I've 100 course material in htm, then i will need to write 100jsp for them? too complex. This problem is troublesome.
22 years ago
Yes, main problem is here-->
Originally, my idea is like yours, and I really build several javas: login.java, Userlog.java (This is user's object, with username and password), other functional java for checking the session. I placed all this java in the same directory, they work. But if those functional java are placed in directories different from login.java (which create the session), ERROR will be occur. So I wonder if I can't do in this way. Everyfile using the session object MUST be in the SAME directory? Correct me if im wrong. thanks
22 years ago
But in web.xml in tomcat dir, I can't find any keyword of "minExemptions"/"maxExemptions".
And, this java has no problem when calling in directly via http://...:8080/.... but problem only occurs when calling via http://.../... with the help of tomcat.
22 years ago
In my homework, i had to build a site, about e-learning. Basic framework is: login, then have several functions and have various link of note and course material, like this: click here to see
I use a so called Model View Controller method: I need to write a servlet as the single point of contact. This controller servlet will check if the user has logged in. If so, it will forward to the main menu page. Otherwise, it will forward to the login page. So all the functions like Student Info, Chat room, after logined will point to http://localhost/servlet/container which check the session first, then redirect to related functions' Servlet (student info,chat room,etc), but my PROBLEM is that: as you see my pic above, if I use these method for my course material on the right, it will be very bulky, because all the courses material will link to the container first, then redirect to another Servlet to resolve the REAL link in Web-inf, (to prevent viewing without login). Do I present my question clearly? If not, I will explain further. Please kindly suggest what way I can solve, thanks!!
22 years ago
Thanks very much.
I will try them later when I've time.
22 years ago
Thanks.
So 'forward' just pass the control to another Servlet? (Also, pass all the request parameter of from original servlet to another servlet too?)
And it seems that include is more flexible?
Thank You.
22 years ago
I linked apache 1.3 to Tomcat 4 on WinXP and ran most sample jsp or servlet given in Tomcat, most of them havent problem EXCEPT the 'Naming' Servlet at http://127.0.0.1/user/servlets/index.html
if I ran through http://127.0.0.1:8080/user/servlet/JndiServlet , fine; but http://127.0.0.1/user/servlet/JndiServlet , error code will return like this::
JNDI lookup failed : javax.naming.NameNotFoundException: Name maxExemptions is not bound in this Context
list() on /comp/env Context :
Binding : mail: org.apache.naming.NamingContext
Binding : minExemptions: java.lang.Integer
Binding : ejb: org.apache.naming.NamingContext
Binding : name3: java.lang.Integer
Binding : foo: org.apache.naming.NamingContext
listBindings() on /comp/env Context :
Binding : mail: org.apache.naming.NamingContext rg.apache.naming.NamingContext@d36dfe
Binding : minExemptions: java.lang.Integer:1
Binding : ejb: org.apache.naming.NamingContext rg.apache.naming.NamingContext@86c730
Binding : name3: java.lang.Integer:1
Binding : foo: org.apache.naming.NamingContext rg.apache.naming.NamingContext@7f242c

Based on my observation, error is caused by "java:/comp/env/maxExemptions",etc, but how I can solve this on linked via Apache? Thanks!!
----------------------------------------
Source Code for Naming Example
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.NamingException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.directory.InitialDirContext;
/**
* Demonstration of the web application environment support.
*
* @author Remy Maucherat
*/
public class JndiServlet
extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();
response.setContentType("text/plain");

Context ctx = null;

try {
ctx = new InitialContext();
} catch (NamingException e) {
out.println("Couldn't build an initial context : " + e);
return;
}

try {
Object value = ctx.lookup("java:/comp/env/maxExemptions)";
out.println("Simple lookup test : ");
out.println("Max exemptions value : " + value);
} catch (NamingException e) {
out.println("JNDI lookup failed : " + e);
}

try {
Context envCtx = (Context) ctx.lookup("java:/comp/env/");
out.println("list() on /comp/env Context : ");
NamingEnumeration enum = ctx.list("java:/comp/env/");
while (enum.hasMoreElements()) {
out.print("Binding : ");
out.println(enum.nextElement().toString());
}
out.println("listBindings() on /comp/env Context : ");
enum = ctx.listBindings("java:/comp/env/");
while (enum.hasMoreElements()) {
out.print("Binding : ");
out.println(enum.nextElement().toString());
}
} catch (NamingException e) {
out.println("JNDI lookup failed : " + e);
}

}
22 years ago
Can you pls tell me what is the difference between 'forward' and 'include'? I read the doc but i dont understand

Originally posted by zb cong:
see follow:
getServletContext().getRequestDispatcher("/xxx.jsp or /xxx.html").forward(req,resp);
or.........................include(req,resp);
or resp.sendRedirect("xxx.jsp or xxx.html");
the three method all can implement your requirement,but the forward(....) and include(....) method run in serverside,the sendRedirect() method send the response to your browser to redirect to other page

22 years ago
Now I've downgrade to Apache 1.3 and it works. I give it up..
22 years ago
oops i m finding where can download a module for apache 2, do you know where i can get it? I'd to downgrade to apache 1.3 to get use with tomcat4 now,how sad im..
btw, im a newbie, what is 'warp connector' used for? thx
22 years ago
I setup 2 days and still can't get it work.
Using webapp module.so and copy libapr.dll to tomcat/modules and windows/system and windows/system32
in apache conf,
I write:
LoadModule webapp_module modules/mod_webapp.so
#AddModule mod_webapp.c
WebAppConnection conn warp localhost:8008
WebAppDeploy examples conn /examples

When startup apache2, it said "Apache HTTP Server error, must be closed", it seems it's libapr.dll, I don't know, have any success in doing this?Thanks
22 years ago