DaFou
asked on
How to call a method from servlet from JSP
Hi All,
This will be my first every JSP page.
All i have right now is this:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
The JSP page should get a QueryString parameter called message and send it to my servlet message.
http://myJavaWeb/sendMessage.jps?message=HelloWorld
How would a JSP page look like that does this.
The package of my servlet is
package Diversity.Java.Web.Servlet .Persisten tHTTPconne ction;
THe class defenition of the servlet is:
public class PersistentHTTPconnectiontH andler extends HttpServlet
This will be my first every JSP page.
All i have right now is this:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
The JSP page should get a QueryString parameter called message and send it to my servlet message.
http://myJavaWeb/sendMessage.jps?message=HelloWorld
How would a JSP page look like that does this.
The package of my servlet is
package Diversity.Java.Web.Servlet
THe class defenition of the servlet is:
public class PersistentHTTPconnectiontH
ASKER
but i want to call the servlet from my JSP and not have the webserver route in incomming request and response object to the servlet.
The method I want to call is:
public static void sendMessageToAlll(String message)
so how from the JSP do i call that method which is in a servlet
The method I want to call is:
public static void sendMessageToAlll(String message)
so how from the JSP do i call that method which is in a servlet
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
when compiling sendMessage.jsp containing this code:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%
String message = request.getParameter("mess age");
Diversity.Java.Web.Servlet .Persisten tHTTPconne ctiontHand ler.sendMe ssageToAll l(message) ;
%>
I get the following error:
C:\Documents and Settings\root\PersitentHTT P\build\ge nerated\sr c\org\apac he\jsp\sen dMessage_j sp.java:46 : package Diversity.Java.Web.Servlet does not exist
The packagage containing the servlet is located in the source package folder of the current project in netbeans 5.0. The sendMessage.jsp is located in the Web Pages folder of the current project in netbeans 5.0
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%
String message = request.getParameter("mess
Diversity.Java.Web.Servlet
%>
I get the following error:
C:\Documents and Settings\root\PersitentHTT
The packagage containing the servlet is located in the source package folder of the current project in netbeans 5.0. The sendMessage.jsp is located in the Web Pages folder of the current project in netbeans 5.0
Please check your classpath in "Library Manager".
ASKER
What should the class path look like?
ASKER
The werid thing is when I begin typing Diversity in my jsp page. It finds it in the dropdown and I can navigate all the way to persistantHTTPhandler.
But once there It does NOT find the public static sendMessageToAll
But once there It does NOT find the public static sendMessageToAll
make sure you have imported " PersistentHTTPconnectiontH andler" and make sure Diversity.Java.Web.Servlet .Persisten tHTTPconne ctiontHand ler is the right path in your jsp.
ASKER
I now have this but it still is not compiling.
<%@ page import="Diversity.Java.Web .Servlet.P ersistentH TTPconnect ion" %>
<%
String message = request.getParameter("mess age");
Diversity.Java.Web.Servlet .Persisten tHTTPconne ction.Pers istentHTTP connection tHandler.s endMessage ToAlll(mes sage);
%>
compile:
Compiling 1 source file to C:\Documents and Settings\root\PersitentHTT P\build\ge nerated\cl asses
C:\Documents and Settings\root\PersitentHTT P\build\ge nerated\sr c\org\apac he\jsp\sen dMessage_j sp.java:6: package Diversity.Java.Web.Servlet does not exist
import Diversity.Java.Web.Servlet .Persisten tHTTPconne ction;
1 error
C:\Documents and Settings\root\PersitentHTT P\nbprojec t\build-im pl.xml:361 : The following error occurred while executing this line:
C:\Documents and Settings\root\PersitentHTT P\nbprojec t\build-im pl.xml:353 : The following error occurred while executing this line:
C:\Documents and Settings\root\PersitentHTT P\nbprojec t\build-im pl.xml:141 : Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 5 seconds)
While every thing excists just fine
<%@ page import="Diversity.Java.Web
<%
String message = request.getParameter("mess
Diversity.Java.Web.Servlet
%>
compile:
Compiling 1 source file to C:\Documents and Settings\root\PersitentHTT
C:\Documents and Settings\root\PersitentHTT
import Diversity.Java.Web.Servlet
1 error
C:\Documents and Settings\root\PersitentHTT
C:\Documents and Settings\root\PersitentHTT
C:\Documents and Settings\root\PersitentHTT
BUILD FAILED (total time: 5 seconds)
While every thing excists just fine
<servlet>
<servlet-name>PersistentHT
<display-name>PersistentHT
<servlet-class>Diversity.J
</servlet>
<servlet-mapping>
<servlet-name>PersistentHT
<url-pattern>/sendMessage.
</servlet-mapping>
This way you can call your servlet class when the url pattern matches the "/sendMessage.jps".