Standard imports of Java Servlets - java

Java Servlets use usually the following import statements:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
If I get it right then javax.servlet.* imports everything inside the package. And because .http is a subpackage of .servlet:
Isn't the third statement unnecessary?
import javax.servlet.* should include .http already.
Or is my assumption wrong. Then please correct me.

No, Java don't do that.
Importing javax.servlet.* imports all of the types in the javax.servlet package but not the types declared in javax.servlet.http.
See tutorial (Apparent Hierarchies of Packages section)

Related

I Added a jar into my Eclipse project, but still it doesn't import

package esta.fontend;
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import esta.dbConnection.ConnectionDB;
import net.proteanit.sql.DbUtils;// This is where i find the problem;
Image Link explaining my problem
rs2xml.jar doesn't contain the needed java class.
The most probably reason is that it was built incorrectly.
As you may see it contains 'src.net.proteanit.sql' which is empty.
So to fix the problem the jar should be rebuilt
The package should not include 'src' prefix
The DbUtils class should be truly included into it.

Jython Java Package Import Statement

I wanted to import a java package "TestPackage" into jython script,
import TestPackage
print TestPackage.SampleClass().getMessage();
I am getting the following error,
ImportError: No module named TestPackage","errorPoint":"","lineNumber":"1","errorPointMessage":"in <module>\nImportError: No module named TestPackage
When I use from TestPackage import SampleClass works fine. If I execute from <pacakage> import <anyclass> statement once, then import <pacakage> works without the import error.
I tried setting PySystemState.add_package("TestPackage"), this works but leading to memory leakage.
Is there any alternative way to make use of import TestPackage ? How can I use "import <packagename>" in jython without memory leakage?
This is due to a difference in Java and Python.
In Java you can import classes, and static import constants and static methods. You cannot import a package. import java.util; is a compile error, but import java.util.Collections; or import java.util.Collections.*; are fine. A Java package is a way of organizing a bunch of classes but doesn't isn't a class with a constructor, methods, or constants. I like to think of a Java package as a zip file containing all the classes that belong to that package. A Java package is merely a namespace.
In Python you can import modules, classes, and from-import classes, functions, and constants. However, a Python module is a lot more like a Java class than a Java package. It is not merely a "zip file of classes", but a class that contains subclasses. A Python module is a collection of scripts in a folder with an init.py, which defines classes and methods for that module. Thus, you can do things like import collections. Python also lets you do import collections.OrderedDict, from collections import OrderedDict, and from collections import OrderedDict as OD.
I hope this explains why you can't import your Java package. You have to import something (probably a class) from that Java package in order for python to create a variable.

How to import my java class into another java class in Xpages java

In my Xpage, I have added to Java classes. One is "AUser" declared in Models package and one is "AUserRepository" declared in a Repository package.
When I try and import AUser into a class in my AUserRepository... the import statement I type in is not working.
import com.Discussion.utils.AUser;
Weird thing is, If I exit out of Notes and load up eclipse standalone, and make the same kind of stuff, the import works fine. Am I missing out some important factor in Xpages?
The problem definitely is the import routing.
import Models.AUser;
import Repository.AUserRepository;
Is the project build path correct? Project - Properties - Java build path. Also, have you tried cleaning the project? Project - Clean....

Importing javax package in Java without an IDE

I'm developing a project in Java using Vim, and I need to use some classes from the javax package. What should I install and configure to be able to import any of them?
I already installed the JDK 7 and also put it as my default using update-alternative command.
For example, when I compile a class (for example one called GUI) that uses the javax package, the compiler displays:
> javac GUI.java
GUI.java:1: error: package javax does not exists
import javax.swing;
^
1 error
I think it is related to the $CLASSPATH environment variable, but I'm not sure how to tell the compiler where the package is (since it is inside the JDK 7).
Instead of trying to import javax.swing, which does not actually specify anything specific, you should import javax.swing.*. This imports everything in the javax.swing package.
import javax.swing.*;
You can also import specific swing components and utilities:
import javax.swing.JButton;
See The Java Tutorials page on Using Package Members for more information about importing package members.
You should be importing as:
import javax.swing.*;
or else for example
import javax.swing.JFrame;

Is this how Java package imports are supposed to work?

I've been struggling with my first regex. During the compile, Pattern and Matcher kept getting cannot find symbol errors.
I just changed import java.util.* to import java.util.regex.* and it works like a dream.
I was under the impression that importing java.util.* would also bring in java.util.*.* etc. Is that not the case? I can't find any documentation that addresses this specific question....
No, package imports only get the direct classes in that package (java.* will not import everything, only ones such as Java.SomeClass, not java.util.SomeClass)
Importing java.util.* will not import java.util.*.*.
Yes, that is how package imports work (and are supposed to work) in Java. For example, doing import javax.swing.*; will import all classes within javax.swing.* but not sub-packages and their classes.
Ergo, javax.swing.* will not import javax.swing.event or javax.swing.event.*
Read the following blog for some friendly newbie advice.
See a link and a quoted excerpt from the link below.
http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
Importing java.awt.* imports all of the types in the java.awt package,
but it does not import java.awt.color, java.awt.font, or any other
java.awt.xxxx packages. If you plan to use the classes and other types
in java.awt.color as well as those in java.awt, you must import both
packages with all their files:
import java.awt.*;
import java.awt.color.*;

Categories