Win a copy of Microservices Testing (Live Project) this week in the Spring forum!

Joe Blant

Greenhorn
+ Follow
since Mar 11, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Joe Blant

Hi Ulf Dittmer,

Thanks for your wise suggestion. I haven't think of this before. I will try it.

Joe Blant
15 years ago
JSP
hi Ulf Dittmer,

Thanks for your suggession.

May i express my intension of this attempt...

My web application contains application level class which connects to certain host, make connection as long as application live.

I load this connecting object at application startup using certain loader class ....
<servlet>
..
<load-on-startup>1</load-on-startup>
</servlet>


And my web service want to access that connection, and send some message to the host connected by my application level object.


I would like to get suggestion for this case.

Thanks,
Joe Blant
15 years ago
JSP
hi Ulf Dittmer ,
thanks for your reply.
my purpose is...

i want to deploy web service which access application level object. I want to access application level object, servlet can access application level object, so i let my class extends servlet. then change my source file to jws file( file.jws) to let AXIS deploy my class as web service. but it fail when i access getServletContext() method outside from container.

Are there any way to access , app object?

thanks,
Joe Blant
15 years ago
JSP
Hi all,

i try


when i run this servlet in console, got exception at calling to getServletConfig() method,NullPointerException come out.

i would like to access application level outside of web container. Are there any way to instantiate ServletConfig to call web application obj.

Thanks,
Kami Kaze
15 years ago
JSP
Hi Sravan Kumar,

Thanks for your reply.

Kami Kaze
15 years ago
JSP
Thank Ulf Dittmer,

When i try this kind of web service in WebSpere Application Developer IDE, it also fails. Are there any other ways to deploy web service like this?

Thanks,
Kami Kaze
15 years ago
Hi all,

my web application contain jsp and support class(Bean).

in jsp i use BeanClass,
in BeanClass, some function read file content just like new File("/resource/file1.txt");
i put BeanClass.class in /WEB-INF/classes folder and jsp file under application context(/).

when i call BeanClass's function from jsp FileNotFound Exception occurs and it search file under %tomcat_directory%\webapps\resource\file1.txt even though i expected the path will be /my_context/resource/file1.txt


I'd like to know that how to point path relatively in BeanClass. I dont want to pass realPath from jsp to the bean. how can Bean will point dependently to the certain file location.


thanks,
kaze
15 years ago
JSP
Hi all,

I wish to develop webservice using axis which access application level object,which live as long as application life time. I want to access ServletContext, so i write my web service class as Servlet ....

//loader servlet to load application object
import javax.servlet.*;
import javax.servlet.http.*;

public class Loader extends HttpServlet{
public void init(ServletConfig conf){
Integer counter = new Integer("0");
conf.getServletContext().setAttribute("cnt",counter);
System.out.println("::::::::::::::::::::::: sucessful!");
}
}

.......
//in web.xml
<servlet>
<servlet-name>Loader</servlet-name>
<servlet-class>Loader</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

...

//here is web service file "CounterWebService.jws"
import javax.servlet.*;
import javax.servlet.http.*;

public class CounterWebService extends HttpServlet{
ServletContext context;
public void init(ServletConfig conf){
context = conf.getServletContext();
}

public String getCounter(String str){
Integer counter = (Integer)context.getAttribute("cnt");
if(counter==null) return "Dont get application object";
counter = new Integer(counter.intValue()+1);
context.setAttribute("cnt",counter);
return String.valueOf(counter.intValue());
}

}

and when i call http://localhost:8080/axis/CounterWebService.jws
tomcat command display the following error
- The class javax.servlet.ServletConfig is defined in a java or javax package an
d cannot be converted into an xml schema type. An xml schema anyType will be us
ed to define this class in the wsdl file.

I'd like to know we can access applcation level object from web service? If then how to access it.


Thanks in advance
kaze
15 years ago
hi Ernest Friedman-Hill

Thanks for your reply.
Hi all,

I would like to write java class that will connet to certain host and port, and stay behind, when connection error occurs, it will detect and try to reconnect silently. how do i make this kind of class for both performance and reliablity of connection.


import java.net.*;
import java.io.*;

public class SolidConnector implements Runnable {
private Socket socket;
private String host;
private int port;
private int reconnectTime = 3000;

...

public SolidConnector(String host,int port){
socket = new Socket();
this.host = host;
this.port = port;
}

public boolean connect(){
try {

socket= new Socket(host,port);
System.out.println ("connected to" +socket.getInetAddress().getHostName());
return true;
}catch (Exception ex) {
//fail
}
return false;
}

public void run(){
int counter = 0;
while(!connect()){
try {
System.out.println ("connecting..."+ ++counter);
Thread.sleep(reconnectTime);

}
catch (Exception ex) {

}
}

while(true){

if(stopped) break;
try {
//nothing do, give cpu to other threads
Thread.sleep(2000);
//i want to detect connection is ok or not
//if connection got problem it throws Connction reset exception
socket.getInputStream().read();

}catch (IOException ex) {
System.out.println ("Connection Lost!");
reconnect();
}catch (InterruptedException ie){

}
}
}

public void reconnect() {
socket = null;
int counter = 0;
while(!connect()){
try {
System.out.println ("reconnecting..."+ ++counter);
Thread.sleep(reconnectTime);
}
catch (InterruptedException ex) {
//interrupted
}
}
}

public void stop(){
this.stopped = true;
}

public static void main (String[] args) {

SolidConnector sc = new SolidConnector("localhost",8000);
Thread t = new Thread(sc);
t.start();

}

}


The key point i like to know is how to detect when connection lost? I dont want to detect by socket.getInputStream().read() method. I would like to detect silently.

Please give suggestions for how to overcome this problem for performance and reliabily issues.

Thanks in advance,
kaze
i send file using tcp socket on LAN.
i want to send all file both ascii and binary files.
some time i send safely, sometime it corrupts in transit.
mostly when i send very small file, it corrupts.


Are there any ways to send safely?
with regards,
kaze

in SenderThread...

FileInputStream fin;
Socket socket = ...
OutputStream netout = socket.getOutputStream();

final int SIZE = 8192;
byte [] data = new byte[SIZE];
int bytesRead = 0;
long bytesSend = 0;
while(( bytesRead = fin.read(data)) != -1) {

while(bytesRead < SIZE) {

int i = fin.read(data, bytesRead, SIZE-bytesRead);
if(i == -1 ) break;
bytesRead += i;
}
netout.write(data,0,bytesRead);
netout.flush();
bytesSend += bytesRead;
}//end outer while


===================================================
in ReceiverThread...

Socket socket =...
InputStream netIn = socket.getInputStream();
FileOutputStream fout =...

final int SIZE = 8192;
byte [] data = new byte[SIZE];
int bytesRead = 0;
long bytesReceive = 0;

while(( bytesRead = netIn.read(data)) != -1) {

while(bytesRead < SIZE ) {
int i = netIn.read(data, bytesRead, SIZE - bytesRead);
if(i == -1 ) break;
bytesRead += i;
}
fout.write(data,0 , bytesRead);
fout.flush();
}