Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

How can I call a Servlet from a JSP page?

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Author
Posts: 350
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use
jsp:forward
 
northfield Sid
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
northfield Sid
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm.....HAve u tried adding the Servlet class name ie <jsp:forward portal/ServletName> ?
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic