• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I call a Servlet from a JSP page?

 
northfield Sid
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I call a Servlet from a JSP page?
I have an JSP page that does a few things but when there is an error I need the JSP page to call a Servlet but do not know how to do it.
I want to call a ServletFailLogin from a JSP page how can I do it?
<html><head>
<title> Login Page </title>
</head>
<body>
<%@ page import= "portal.* " %>
<jsp:useBean class="portal.PortalSystem" id="bean" scope="session" />
<% try
{
String servername = request.getParameter("servername");
String username = request.getParameter("username");
String password = request.getParameter("password");
ftpbean.ftpConnect(servername, username, password);
}
catch(Exception e)
{
"http://localhost:8080/test/servlet/ServletFaillogin";
}
%>
...
<h1> You have successfully logged in. </h1>
...
</body>
</html>
 
Rick Hightower
Author
Ranch Hand
Posts: 350
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use
jsp:forward
 
northfield Sid
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
northfield Sid
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using jsp:forward as an JSP tag but getting errors:
Please help

Here is the error:
org.apache.jasper.JasperException: Unable to compile Note: sun.tools.javac.Main has been deprecated.
C:\tomcat\jakarta-tomcat-3.3.1\work\DEFAULT\myJSPs\jsp\portal_0002dproject\processftplogin_1.java:94: Missing term.
{
^
C:\tomcat\jakarta-tomcat-3.3.1\work\DEFAULT\myJSPs\jsp\portal_0002dproject\processftplogin_1.java:95: Missing term.

^
C:\tomcat\jakarta-tomcat-3.3.1\work\DEFAULT\myJSPs\jsp\portal_0002dproject\processftplogin_1.java:95: Missing term.

^
C:\tomcat\jakarta-tomcat-3.3.1\work\DEFAULT\myJSPs\jsp\portal_0002dproject\processftplogin_1.java:95: ';' expected.

^
4 errors, 1 warning
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to put a JSP tag within a Scriptlet. Anything within the <% %> must be Java code. You must either end the scriptlet, put the tag, and then begin the scriptlet again or use a RequestDispatcher to forward. Under the hood the jsp:forward tag is using a RequestDispatcher anyways. Ultimately the choice is yours.
 
northfield Sid
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I have changed the line of code to:

I think this could be becuase of the line
" "
All my html and JSP pages are at:
C:\tomcat\jakarta-tomcat-3.3.1\webapps\myJSPs-XML\jsp\portal-project
My class files are at:
C:\tomcat\jakarta-tomcat-3.3.1\webapps\myJSPs-XML\WEB-INF\classes\portal
within the folder portal I have the class:
"ServletFaillogin.java"
But then there is an error loggin into system it does not display my ServletFaillogin page. Instead gives me:
HTTP 404 - File not found
Internet Explorer
Why is this???
 
northfield Sid
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I have tried 3 version. I have managed to make one version work using the redirect as suggested but not with the other two why???
response.sendRedirect("http://localhost:8080/myJSPs/servlet/portal.ServletSetDirectoryError");
This works. But the others do not why???
When I do
request.getRequestDispatcher("http://localhost:8080/myJSPs/servlet/portal.ServletSetDirectoryError").forward(request, response);
I get a blank file in the browser and in the status bar the word "DONE"
I have also tried another style:
<jsp:forward page="/myJSPs/servlet/portal.ServletSetDirectoryError" />
Here is get the same blank file and in the status bar the word "DONE"
Why???
 
Vedhas Pitkar
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm.....HAve u tried adding the Servlet class name ie <jsp:forward portal/ServletName> ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!