If you want to access local system resources using java applet you may get error something like bellow:
java.security.AccessControlException: access denied (java.util.PropertyPermission user.home read)
To solve this problem you need to signed your applet. Bellow the steps describe the creation of a self-signed applet. This is useful for testing purposes. For use of public reachable applets, there will be needed a “real” certificate issued by an authority like VeriSign or Thawte.

The applet needs to run in the plugin, as only the plugin is platform- and browser-independent. And without this indepence, it makes no sense to use java…

1. Create your code for the applet as usual.
It is not necessary to set any permissions or use security managers in
the code.

2. Install JDK 1.3
Path for use of the following commands: [jdk 1.3 path]\bin\
(commands are keytool, jar, jarsigner) Password for the keystore is any password.

3. Generate key:
keytool -genkey -keyalg rsa -alias tstkey
Enter keystore password: *****
What is your first and last name?
[Unknown]: Your Name
What is the name of your organizational unit?
[Unknown]: YourUnit
What is the name of your organization?
[Unknown]: YourOrg
What is the name of your City or Locality?
[Unknown]: YourCity
What is the name of your State or Province?
[Unknown]: YS
What is the two-letter country code for this unit?
[Unknown]: US
Is CN=Your Name, OU=YourUnit, O=YourOrg, L=YourCity, ST=YS, C=US
correct?
[no]: yes

(wait…)

Enter key password for tstkey
(RETURN if same as keystore password):

(press [enter])

4. Export key:
keytool -export -alias tstkey -file tstcert.crt

Enter keystore password: *****
Certificate stored in file tstcert.crt

5. Create JAR:
jar cvf tst.jar tst.class
Add all classes used in your project by typing the classnames in the
same line.

added manifest
adding: tst.class(in = 849) (out= 536)(deflated 36%)

6. Verify JAR:
jar tvf tst.jar

7. Sign JAR:
jarsigner tst.jar tstkey
Enter Passphrase for keystore: *****

8. Verifiy Signing:
jarsigner -verify -verbose -certs tst.jar

9. Create HTML-File for use of the Applet by the Sun Plugin 1.3
(recommended to use HTML Converter Version 1.3)
You can use this signed jar in applet like bellow:

applet code=AppletClass.class
archive=tst.jar
width=200 height=100

10. Delete existing certificate:

keytool -delete
 -alias keyAlias
 -keystore keystore-name
 -storepass password

Source: forums.sun.com

java.lang.ClassCastException: org.apache.catalina.util.DefaultAnnotationProcessor cannot be cast to org.apache.AnnotationProcessor
This error may gives if jar conflict or duplicate in build path of wepapp.To solve this error need to remove conflicted jars

Create an oracle function:

Suppose you have created an oracle function by executing the following code in oracle

create or replace FUNCTION "FUN_GET_EMPLOYEE_NAME "( P_EMPLOYEE_ID           VARCHAR2
)
RETURN VARCHAR2
AS PRAGMA AUTONOMOUS_TRANSACTION;
…………………………………………
Write rest of code to return data.
…………………………………………

Function description:

This pl/sql code will simply create a function named “FUN_GET_EMPLOYEE_NAME” which will take a VARCHAR2 input parameter as P_EMPLOYEE_ID and return a VARCHAR2 data i.e. Employee name.

Java program to access this oracle function and get the return data from this oracle function:

Connect to oracle data base by modifying the following code:

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
java.sql.Connection connection = java.sql.DriverManager DriverManager.getConnection("jdbc:oracle:thin:@"+ "DBServerIP/localhost"+":1521:DBName","DBUser", "DBPassword");
} catch (Exception e) {
e.printStackTrace();
}

Now create a java.sql.CallableStatement by using this java.sql.Connection to call the oracle function as follows:

java.sql.CallableStatement cstmt = connection.prepareCall("{?=call FUN_GET_EMPLOYEE_NAME (?)}");

As our function needs one input parameter to call the oracle function so here I gave (?) to indicate it, if you need to pass more parameters just adjusts it with your parameters number.

Now register the out parameter of the oracle function if any as follows:

cstmt.registerOutParameter(1,  java.sql.Types.VARCHAR);

If your function return more parameter just add them sequentially 2, 3 …

Now add the input parameters of oracle function in your java code as follow:

cstmt.setString(2, “EMPLOYEE_ID”);

Note here we added one parameter because our oracle function take one parameter as input and index is set to 2 as we have already set our oracle function out parameter to index 1 in java code above. You can also set parameters as you needed and with different data type. You can also use parameter name instead of index cstmt.setString(parameterName, x)or cstmt.setInt(parameterName, x) whatever you want, In this case you should set all parameter by parameter name otherwise set all parameter by index.

Now call the execute function of CallableStatement to execute this statement as follows:

cstmt.execute();

You can get return data from your oracle function in java code as follows:

cstmt.getString(1)

Here 1 mean the out parameter index.

What are Java Servlets?

Servlets are snippets of Java programs which run inside a Servlet Container. A Servlet Container is much like a Web Server which handles user requests and generates responses. Servlet Container is different from a Web Server because it can not only serve requests for static content like HTML page, GIF images, etc., it can also contain Java Servlets and JSP pages to generate dynamic response. Servlet Container is responsible for loading and maintaining the lifecycle of the a Java Servlet. Servlet Container can be used standalone or more often used in conjunction with a Web server. Example of a Servlet Container is Tomcat and that of Web Server is Apache.

Servlets are actually simple Java classes which must implement the javax.servlet.Servlet interface. This interface contains a total of five methods. Most of the time you don’t need to implement this interface. Why? Because javax.servlet package already provides two classes which implement this interface i.e. GenericServlet and HttpServlet. So all you need to do is to extend one of these classes and override the method(s) you need for your Servlet. GenericServlet is a very simple class which only implements the javax.servlet.Servlet interface and provides only basic functionality. On the other hand, HttpServlet is a more useful class which provides methods to work with the HTTP protocol. So if your Servlet works with HTTP protocol (in most cases this will be the case) then you should extend javax.servlet.http.HttpServlet class to build Servlets and this is what we are going to do in this article.

Servlets once initialized are kept in memory. So every request which the Servlet Container receives, is delegated to the in-memory Java Servlet which then generates the response. This ‘kept in memory’ feature makes Java Servlets, a fast and efficient method of building Web Applications.

Requirements to run this sample application:

  1. Download and install jdk from (http://java.sun.com/javase/downloads/index.jsp) I have used jdk 1.6.0_14 for this example.
  2. Downlad and install Tomcat from (http://tomcat.apache.org/) I have used Apache Tomcat-6.0.20 and my installation directory C:\apache-tomcat-6.0.20

Write and Run Your Java Servlet:

1. Create Your Servlet:

Create a java file named “MyServlet.java” in C:\apache-tomcat-6.0.20\webapps\myapp\WEB-INF\classes\com\myapp\servlet folder.

2. Write code for your servlet

Put the following code in Myservlet.java


package com.myapp.servlet;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {

res.setContentType(“text/html”);
PrintWriter out = res.getWriter();
out.println(“”);
out.println(“MyServlet”);
out.println(“\t“);
out.println(“”);
out.println(“”);
out.println(”

This is my first servlet out put.

“);
out.println(“”);
out.close();
}
}

Explanation:

Our TestServlet is extremely simple. It displays the current date and time on the server to the user.

We overrode just one method of HttpServlet class. doGet() is called when a HTTP GET request is received by the Servlet Container. In this method we set the content type of our response to ‘text/html’ (informing the client browser that it is an HTML document). Then we get hold of PrintWriter object and using its println() method, we send our own HTML content to the client browser. Once we are finished, we call its close() method

  1. Compile your written servlet by writing the following command in command line


cd C:\apache-tomcat-6.0.20\webapps\myapp\WEB-INF\classes\com\myapp\servlet
javac -cp %CATALINA_HOME%\lib\servlet-api.jar MyServlet.java

Here I have loaded servlet-api.jar to classpath, if you did not added CATALINA_HOME as environment variable you can simply write the tomcat lib path here I mean in our case C:\apache-tomcat-6.0.20\lib
If no error it will create a MyServlet.class file after executing this command.

3. Write web.xml for your application:

web.xml file, also known as “Web Deployment Descriptor” allows us to configure our web application inside the Servlet Container. Here we can specify the name of our web application, define our Java Servlets, specify initialization parameters for Servlets, define tag libraries, and a whole lot more. This file is placed inside the /WEB-INF folder.

Add the following text in your web.xml file to run your servlet:

<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                               http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
               version="2.5">  
               <servlet>
                               <servlet-name>MyServlet</servlet-name>
                               <servlet-class>com.myapp.servlet.MyServlet</servlet-class>
               </servlet>
               <servlet-mapping>
                               <servlet-name>MyServlet</servlet-name>
                               <url-pattern>/MyServlet</url-pattern>
               </servlet-mapping>
</web-app>

4. Finally Run your servlet:

Start/ Restart your tomcat and point your browser to http://localhost:8080/myapp/MyServlet if everything ok it will print “This is my first servlet out put.” in browser

Got great help from this source

Steps of  adding external jar or folder in NetBeans project which I have followed:

1. Go to project properties by right clicking on project.

2. Then click on Libraries tab, You will see Compile, Run, Compile Tests, Run Tests tabs.

3. Click on Compile tab

4. Click on Add JAR/Folder button at right

5. Then browse and select the jar files or folder which you want to include. Included jar files or libraries will show on  the following box of Compile tab.

6. Click on OK button.

7. Finished.

 

In windows when run tomcat:undeploy maven command it can not delete some library which locked or using by tomcat such as xwork-2.0.4-sources.jar,  struts2-core-2.0.11.1.jar, struts2-spring-plugin-2.0.11.1.jar, struts2-codebehind-plugin-2.0.11.1.jar

I have solved this problem by using antiJARLocking=”true” antiResourceLocking=”true” in context.xml of tomcat. That mean I have replaced

<Context reloadable=”true” antiJARLocking=”true” antiResourceLocking=”true”>

in place of the default

<Context reloadable=”true”>

It also work same if I put a context.xml file in WEB-INF folder containing the changes which have done above  in tomcat’s context.xml file.

May 2024
S M T W T F S
 1234
567891011
12131415161718
19202122232425
262728293031  

RSS Hima’s Blog

  • An error has occurred; the feed is probably down. Try again later.