• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JSP to call Servlet

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay first of all to introduce myself!
My name is Kostas, I live in Greece and I am in the final semester in the Computer Science departement of Pireaus University(Strange name isn't it?, Pireaus is the largest port in Greece and the 3rd biggest worldwide)

Not until recently I got a project for my University which I had to write in JSP. So far, only have gotten myself into the paths pf PHP, I needed a good reference book for JSP & Servlets. For my good luck I bought Head First Servlets & JSP. I only write this because I know that one of the founders of the site is one of the writers of the book.

Well to be honest, after seeing so many posts about troubles, I didn't want to bring my troubles here too. But if anyone wants to help I will gladly appreciate it.

-----------------------------------------------------------------------------------------
My main problem is that I am trying to connect a JSP and its servlet. Well the problem is that the JSP doesnt find the Servlet at all. I use GlassFish server v3 and NetBeans IDE 6.8v (which IMHO is WAAAY better than Eclipse). Here are the exact steps I put myself into

(I will describe all the steps and the minor details so as to diminish any assumptions brom being made)

1.Create the Project

Here I use an external disk drive (F:/) where all my projects are stored in ([12] NetBeans Workspace/) folder.
Okay here goes:
New Project->Java Web->Web Application

ProjectName: AcidLabs
ProjectLocation: F:\[12] NetBeans Workspace
ProjectFolder: F:\[12] NetBeans Workspace\AcidLabs

No | UseDeticated Folder for Storing libraries
Yes | Set as Main Project

Server: GlassFish v3 Domain
Java EE6 Web

Framewroks: Only JavaServer Faces

(Using the default library and at configuration preferred language is JSP)

2.Creation of JSP

At Web Pages folders I create J_Login.jsp file.
The code is this:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<f:view>
<html>
<head>

<title>Login Page</title>

</head>
<body>

<form method="POST" action="/login.serv">
<input type="TEXT" name="I_Login" /> <br>
<input type="SUBMIT" value="Login"/>
</form>

</body>
</html>
</f:view>

3.Creation of Servlet (File->New File->Servlet)

ClassName: S_Login
Project: AcidLabs
Location: Source Packages
Package: com

(next page will be in the deployment descriptor)
The Servlet code is this:

package com;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class S_Login extends HttpServlet
{

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {

String c = request.getParameter("I_Login");

request.setAttribute("resp",c);
RequestDispatcher view = request.getRequestDispatcher("J_Lessons.jsp");
view.forward(request, response);

} finally {
out.close();
}
}

}

The code is doing nothing than forwarding the parameter from J_Login.jsp

4.The other JSP

The JSP that hears from the servlet is this

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
${param.resp}
</body>
</html>


5. The web.xml

In my web.xml the following 2 structs exist
<servlet>
<servlet-name>CustomServlet</servlet-name>
<servlet-class>com.S_Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CustomServlet</servlet-name>
<url-pattern>/login.serv</url-pattern>
</servlet-mapping>

Running the J_Login.jsp executes the page fine and on submit I get this error

HTTP Status 404 -
type Status report
message
descriptionThe requested resource () is not available.
GlassFish v3

if i call the servlet on the form element by simple "login.serv" without the "/" I get this error which is even worse

HTTP Status 404 - /login.serv not found
type Status report
message/login.serv not found
descriptionThe requested resource (/login.serv not found) is not available.
GlassFish v3

and the servlet is not even found....

I know the servlet is not compiled but I did a previous project experimenting with OpenFaces 2.0 and everything worked fine by the same steps. I wanted to do a fresh start and now I get all these.

Sorry to trouble you. Any help will be much appreciated!!!
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi kostas,

most of the things seems to be ok in your example. but do a quick check on below points.

1. Check if the S_Login servlet is called first by including System.out.println("ya came in servlet");
as the first line in the dopost method. This is because i doubt that servlet is called fine but dispatcher is not working


2. Dispatcher may not have worked because in the below jsp (J_lessons.jsp) what is
${param.resp}, it never looks syntax of a normal html or jsp. where is this param variable or attribute
better remove this ${param.resp} and try out again.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
${param.resp}
</body>
</html>


I will further explore on this and would post you if i get any good results.
Do reply if you have any more comments to add on.

have a nice day.

By,
Ibrahim
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags.
 
konstantinos marinis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oooops sorry about CodeTags... didnt know...

Well, I removed the EL expression


but still nothing changed... Same error messages.

For me it seems the problem begins even before the Dispatcher.
I placed an

alert, and it didn't show up. The error comes up so fast, that it seems like the servlet doesn't even get into the code, althought this might be a misleading assumption!
 
konstantinos marinis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, for now I solved it. I just returned to Tomcat and use JAVA EE5 this time. I don't really know but are the secifications for servlets changed in EE6 ?
As far as I know, Servlets 3.0 ase they are supposed to be called in EE6, are more compatible than the other versions... I don't think it's Glassfish either... Maybe it's the JSF framework implementation wich I now disabled it...

All I now for now is that it works sweet...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic