Link to home
Start Free TrialLog in
Avatar of quicksystem
quicksystem

asked on

I have a server and a client in a WEB JAVA APPS How can i make that the client prints to and not only the server?

I have a server with a printer and a client with its own printer, the cliente is calling the server by an IP like http://<<IP>>:<<port>>/<<instance>> i have a print function and it works perfectly in the server but when i call it from the client it doesnt print with the localhost printer of the client instead of that the server resolve it and print in the server printer.

I need a example code please i try it everythign.

notes:  
  Language JAVA
  The printing must by directly, if the printer dialog box ( like ctl-P in Microsoft word) appears it doesnt work for me.
  The content wich will be printed is a string not a file

regards

Mark
Avatar of girionis
girionis
Flag of Greece image

If the IP resolves to the printer on the server then I'd say you need to look at your network configuration. This hasn't got to do with Java. What does the network admin say?
Avatar of quicksystem
quicksystem

ASKER

I think i didn't explain my proble clearly, so here we go again:

I have a server "myServer" with an application, then i have 2 clientes "client 1" and "client 2" like in the followin example:

MY SERVER
    MYSQL
    JDK
    TOMCAT
    MY WEB APPLICATION(ALL JAVAS AND JSP'S)
    IE6
    PRINTER

CLIENTE 1                      CLIENT 2
   IE6                                  IE6
   PRINTER                          PRINTER


Now if i want to run my application in the server i just use http://localhost:8080/myApplication/printTicket.jsp ( this jsp have only a request to a class who send the String data (not a file) to the printer) and the localhost (myServer) printer prints the string data ( for example hello word).

Then if I want to run this application in one of my clients i need to open a web browser and in the URL types http://myserver:8080/myApplication/printTicket.jsp when i press enter the client send the request to the server to solve it. When the server finished send to the client the response. (JUST HTML)

Now if i run this in the client, the printer that the request use is the server printer because the server resolves the request (and not the client). So the localhost (the client printer) didn't print any thing.

What i'm trying to do?
That the client send the request to the server and the server gets the client printer and print in it, not in the server printer.

One way to solve this proble is using the printer dialog to select the prefered printer ( like when we press ctrl-p in Microsoft Word) but this not solve my problem 100%. Cause the request i have is to print directly with no adicioanl dialogs boxes.

Regards

Mark






   

What you could do then is to get the IP of the client that issued the request. You can use the getRemoteAddr()  method of the request object. You then check the IP. If the IP is the same as the server's IP then you print to the server printr, otherwise you print to the client printer. But the issue I see here is how the server could resolve the printer on the client.

Another thing is that you might be able to do it by using JavaScript, but I am nto sure about that.

WHy do you not want to have the printer popup box show up? This is exactly what it's used for.
Girionis now we are getting close to the solution, the reason i don't what the printer popup box is because is a client request. ( So if the client pays they rules, and our job is create dreams thats why we all like to program jejeje).

I need to know how to set a remote address to select the printer. I show you the pice of code i use to make this task.

Perhaps ( and the must porbably) we need to change this lines. But i have been trying and this is the only way i found. Of Course you are an expert an maybe can help to fix it using an IP (request objetc), the hostname (request objetc) and the printer name (default name and in hardcode).

I found an example , but is a file print example and it doesnt work for me because i print a string( well i think not 100% fo sure)

//send the print job to a network printer.
    //were Au-4022 is the hostname and hp1100 the printer
     ...new FileOutputStream("//Au-4022/hp1100");


could you help me we a pice of code?


Regards

Mark
/*
    DETECTIN PRINTER
 
*/
 
// We create the Print job object   
   PrinterJob job = PrinterJob.getPrinterJob();
                  //We search all the posible printers
                    PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
                  //If there are only 1 printer the rule says is the one, no matters what.        
                   if(services.length == 1){
                        job.setPrintService(services[0]);
                    }else{
                  //other wise we look for the QSPRINT prefix ( thats the one we need)
                        for (int l = 0; l < services.length; l++) {
                            if(services[l].getName().substring(0,7).equalsIgnoreCase("QSPRINT")){
                                job.setPrintService(services[l]);
                            }
                        }
                    }
 
/*
 FORMATING THE PRINT THE VARIABLE TICKET IS AN STRING
*/
                    // Hacemos imprimible el objeto ObjetoAImprimir
                    f = new PageFormat();
                    Paper p = new Paper();
                    p.setImageableArea(0, 0, p.getWidth(), p.getHeight());
 
                    p.setSize(0, 0);
                    f.setPaper(p);
 
 
                    job.setPrintable(new ObjetoAImprimir(), f);
                    //JUST MAKING THE FORMAT
                    lineas = StringVerify.tokenizerTicket(ticket,"\n");
                    page = 1;
                    column = 0;
                  //SEND TO THE PRINTER
                    job.print();

Open in new window

>//send the print job to a network printer.
>    //were Au-4022 is the hostname and hp1100 the printer
>     ...new FileOutputStream("//Au-4022/hp1100");

This should work, since a file is just a resource on the network. You could print to a printer by using a PrintStream that is using a FileOutputStream.




FileOutputStream fos = new FileOutputStream("//Au-4022/hp1100");
PrintStream ps = new PrintStream(fos);
ps.print(<your object>);
ps.flush();
ps.close();

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks the solition was implemented.


Rgards

Mark
Thanks for accepting, glad I was of help :)