I am new to java and jsp. I have just created a small package and that package contains a simple java class. Let me show what i really want to do. I am not working with servlets so please tell me about this simple example so that i can go ahead with my work. Below is my code or a package class and a jsp page.
my java package
package mypack; //this is my package
public class Abc{
public Abc(){
}
public void message(){
System.out.println("My first java package");
}
}
index.jsp this is my jsp page where i need to use this package
<%# page import="java.sql.*"%>
<%# page import="mypack.*"%>
<%
Abc a = new Abc();
a.messsage();
%>
I was using JDK1.5 and tomcat 3. But i want to update my system. Now i have JDK1.7.0_11 and tomcat 7. I know the path of tomcat 3 to put my packages but i don't know where to put these packages in tomcat 7.
tomcat 3 directory path to place packages:
D:\Java\tomcat\examples\WEB-INF\classes //i put my package at this path in tomcat3
tomcat 7 directory path to place packages:
D:\web\Tomcat 7.0\webapps\ROOT\WEB-INF //trying to put my package in here but no use.
I could not find the classes folder under the direcoty WEB-INF in tomcat 7. I made a folder myself named classes inside of WEB-INF but it does not work. Even i have deleted that my "classes folder" and put my package in WEB-INF, but it does not work. Please tell me the path where i can put my java package in tomcat server 7.
I have placed my jsp page in here:
D:\web\Tomcat 7.0\webapps\ROOT\a //folder "a" contains my jsp file. index.jsp and
its working
Problem is, jsp page could not find the package. Please help me out with this.
packages are always located in WEB-INF/classes, not directly in WEB-INF.
Try to put them here thus:
D:\web\Tomcat 7.0\webapps\ROOT\WEB-INF\classes
Related
I have configured Jetty at my Project (Eclipse Neon).
I've created index.jsp under WebContent folder, and my Java class is under WebContent/WEB-INF/classes/foo. The class name is FooClass.java.
At index.jsp, I've set this:
<jsp:useBean id="R1" class="foo.FooClass"/>
Eclipse is alerting me on the useBean tag saying foo.FooClass is an undefined type. What have I done wrong?
Unless WebContent/WEB-INF/classes is listed on the project's Java Build Path as a Class Folder or a Source Folder, and not just an output folder that you've added the class file to yourself, it won't be found by anything the project attempts to compile or validate.
This question already has an answer here:
How specify path to .JSP file for request.getRequestDispatcher()?
(1 answer)
Closed 6 years ago.
I have a servelet called RegisterUser mapped as /register this servelet includes a jsp file called register.jsp . here is the structure of the project :
inside the servelet this code cannot find the jsp file :
RequestDispatcher view = request.getRequestDispatcher(registerView);
and the content of registerView is :
private static final String registerView = "../web/WEB-INF/views/Register.jsp";
It worked fine on eclipse but when I switched to Intellij it shows that the file cannot be found .
Liferay has a somewhat strange layout for it's web-inf and html folders. In Eclipse I never really code path completion to work well in JSPs because it always looked at the wrong spot for files. In IntelliJ I just setup different facets that will help the IDE help me. Here is an example. I have a facet called WEB-INF. In Liferay, when you reference a file in WEB-INF (tagfiles are a GREAT example here) you do it like this:
<%# taglib prefix="showMore" tagdir="/WEB-INF/tags/wj/showmore" %>
The problem there is that by default, the IDE wants to autocomplete like this:
<%# taglib prefix="showMore" tagdir="/docroot/WEB-INF/tags/wj/showmore" %>
I simply made a web facet and said that when I type /WEB-INF I really want you to look at /docroot/WEB-INF. This small change saves me so much headache throughout the day. I also did the same thing with /html instead of /docroot/html.
You know how in eclipse when you go to an init.jsp file in the ext environment it always freaks about the init.jsp includes from portal because it can't find it? I once again used a web facet (called portal-web) and I linked / to /liferay/builds/4.3.x/portal-web/docroot. Now when I say:
<%# include file="/
right there it starts autocompleting. It lists out everything in both portal-web and ext-web for me, because I have access to both inside Liferay! Doing this then:
<%# include file="/html/common/init.jsp" %>
it's a problem, I can even click init.jsp and press ctrl (cmd) + b and it will jump me right into the init file in portal.
I tend to run IntelliJ with one "Module" and two "Content Roots". One content root is portal, the other is ext. The plus here is that the debugger knows about the portal source files, and I can easily jump between jsp files as well.
Resource Link:
Why I use IntelliJ instead of Eclipse?
My JSP instantiates a MovieDetails class. I am currently working using scripting to ensure everything is working fine, before I move to tags.
I am using Tomcat 8.0. Directory structure for MovieDetails.class: com/library/model/beans. A copy of the directory structure with the MovieDetails.class file is also placed under WEB-INF/lib (have tried putting a .jar for the file too)
In the JSP I have:
<%# page import="com.library.model.beans.*" %>
And later:
<%
MovieDetails movDet = (MovieDetails)request.getAttribute("MovieDetailsBean");
....
....
%>
I am getting:
java.lang.NoClassDefFoundError: com/library/model/beans/MovieDetails
Can anyone please tell me why the JSP can't find the class in spite of the class being in the /lib directory?
NoClassDefFoundError and ClassNotFoundException is two different exception
Actually i try to create my first servlet and open it in a browser. But i alway get a 404 error.
My webapps folder from tomcat directory looks like:
docs
examples
host-manageR
manager
ROOT
test
index.htmL
servlet
bin
test.class
src
test.java
.classpath
.project
WEB-INF
classes
my index.html:
<html>
<body>
<div style="width:500px;height:300px;margin:auto;background-color:#EEEEEE;padding:0px 10px 0px 10px;margin-top:50px;">
<h1>Send Text</h1>
<form action="servlet/bin/test" method="get" style="font-size:10px;">
<textarea rows="10" cols="59" name="input"></textarea>
<input type="submit" style="margin-top:20px;"></input>
</form>
</div>
</body>
and this is my test class:
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
#WebServlet(urlPatterns={"/servlet/bin/test"})
public class test extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.println("this is First servlet Example ");
}
}
I know it does nothing with the input from the html form but at this point I just want to open it and see the text inside^^... But when i open:
http://localhost:8080/test/index.html
, fill in the form and countinue or when i open:
http://localhost:8080/test/servlet/bin/test
directly, i always get a 404 error with everything i tried. I would prefer a solution without a xml but i think the problem is not there.
There are several problems in this code.
You're not respecting Java naming conventions. Classes should always start with an uppercase letter. They should never be in the default package.
You're deploying your whole development environment (sources, etc.), instead of generating a correct war file or exploded war file, and to deply that. The structure of your web-app should be
test
any static file you want
WEB-INF
classes
.class files, organized in directories strictly matching the package hierarchy
lib
jar files needed by your application
You're confused about what
#WebServlet(urlPatterns={"/servlet/bin/test"})
does. The url patterns is the set of URLs that you would like to use to access your servlet. The value doesn't have anything to do with the actual location of the servlet class on the disk. If you want the URL of your servlet to be /hello, then you simply use
#WebServlet(urlPatterns={"/hello"})
or, even simpler:
#WebServlet("/hello")
Once deployed, the complete URL of the servlet will thus be:
http://localhost:8080/test/hello
/test being the context path your webapp which is, by default, the name of the war file of exploded war directory you put under tomcat/webapps.
Try to follow these tutorials in Java-Brains for JSP & Servlets its very simple and explain every things from scratch :)
I think after all the comments the problem is just that your
web archive is not even registered within the server.
Try to package everthing under test as a zip file and rename it to test.war.
A war file is nothing more as a zip file with ending "war" for web archive
which indicates that server should resolve the archive as a web application.
if you extract this archive it should look like this.
index.html
WEB-INF
web.xml
|- classes
|- test.class
|- lib
Deploy this file using the management console of tomcat and manually deploy it to your server.
Additionally have a look about Webarchives "war".
If the servlet is registered within the server correctly than you reach it using
the specified pattern which you declared within #WebServlet Annotation.
The context path applies to the name of you war archive.
http://localhost:8080/<name of the warfile>/<urlPattern>
http://localhost:8080/test/servlet/bin/test // if the given example still applies
I'm trying to run old servlet under resin. I have deployed it as a war file. After starting resin there is a dir ic. It contains Webcontent dir with WEB-INF/lib/ic.jar (fatjar), jsp etc. While extracting this ic.jar I see there package com/x/y/z but while trying to access servlet page I'm getting an error: package com.x.y.z does not exist.
Can anyone give me any clue?
The jar needs to be in WEB-INF/lib, not in WEB-INF.
When extracting the JAR file, you should see
com/x/y/z/ClassName.class
not
com.x.y.z/ClassName.class
Assuming that what you wrote wasn't a summary of what you saw (or a typo).