File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Simple Servlet / JSP Prob Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Simple Servlet / JSP Prob" Watch "Simple Servlet / JSP Prob" New topic
Author

Simple Servlet / JSP Prob

Baka Neko
Greenhorn

Joined: Dec 09, 2003
Posts: 14
Hey all
I�m kinda new to all this Servlet / JSP Stuff and I�m even not quite sure, if this belongs in the right forum, but I hope someone in here can help me out.
I got this simple HelloWorld-Based Jsp/Servlet Prob. The JSP should "call" the servlet, the servlet should generate a simple message and send it to the JSP.
Heres my code:
Servlet:
package = mein.packetchen.ui.toll;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;

public class HelloWorldServlet extends HttpServlet {

private ServletConfig config;

public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
System.out.println("bin in doGet");
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

System.out.println("jetzt in doPost");
String theMessage ="Hi!";
String target ="/hello.jsp";
req.setAttribute("message", theMessage);
RequestDispatcher rd;
rd = getServletContext().getRequestDispatcher(target);
rd.forward(req,res);
}
}

JSP:
<%@ page language="java" %>
<%@ page import="java.lang.*" %>
<%@ page import="mein.packetchen.ui.toll.*%>
<%String msg = (String)request.getAttribute("message"); %>
<html>
<body>
<%System.out.println(msg);%>
<%= msg %>
</body>
</hmtl>
Everytime Im calling the JSP Im getting back NULL for the String.

Any help will be appreciating
TIA
Baka out
boobalan chinnasamy
Greenhorn

Joined: Nov 07, 2003
Posts: 4
i can help u.but i want to know is whether the servlet is called from the same JSP or from some other JSP.tell me this i will help u out.
Baka Neko
Greenhorn

Joined: Dec 09, 2003
Posts: 14
Thx for your reply
Im not sure, what�s the easiest way, but atm. I only got 1 JSP that is calling the servlet and is recieving the String.
Baka out
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
It looks like to me, that you have the flow backwards. You stated that the
The JSP should "call" the servlet

I don't see anywhere in your hello.jsp where the servlet is being invoked.
But it actually looks like your servlet is forwarding the request/response object to the hello.jsp.
How are you accessing the servlet? Are you accessing the servlet thru a specific url servlet-mapping from your web-xml(deployment descriptor)?
If you are accessing the hello.jsp directly, then yes you should receive a null string in return because the servlet has not set the string attribute in request scope and forwarded the object to your hello.jsp page i.e.

I hope this makes since.
Craig
Baka Neko
Greenhorn

Joined: Dec 09, 2003
Posts: 14
^^
I�ve just solved my prob..not the way I would like it in the first place, but its working now.
a new JSP called hello1.jsp:
<%@ page language="java" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Lomboz JSP</title>
</head>
<body bgcolor="#FFFFFF">
<form method="post" name="test" action="HelloWorldServlet" >
<input type="text" name="test" Value="test" size="10">
<input type="submit" value="Dr�ck Mich" >
</form>
</body>
</html>
new enhanced Servlet
public class HelloWorldServlet extends HttpServlet {

private ServletConfig servletConfig;
private ServletContext servletContext;

public void init(ServletConfig config) throws ServletException {
super.init(config);
servletConfig = config;
servletContext = servletConfig.getServletContext();


}

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
System.out.println("bin in doGet");
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

System.out.println("jetzt in doPost");
String test;
test = req.getParameter("test");
System.out.println(test);
String target ="/hello.jsp";
req.setAttribute("message", test);
RequestDispatcher rd;
rd = getServletContext().getRequestDispatcher(target);
rd.forward(req,res);

}
}
I can send any String to the Servlet now and it�ll show up in the hello.jsp.
Even better than a simple "HelloWorld"

Many thx for your suggestions and help.
Baka out
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Servlet / JSP Prob