Solved

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?

Posted on 2008-06-16
8
380 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:quicksystem
  • 5
  • 3
8 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 21808255
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?
0
 

Author Comment

by:quicksystem
ID: 21810436
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






   

0
 
LVL 35

Expert Comment

by:girionis
ID: 21811053
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.
0
 

Author Comment

by:quicksystem
ID: 21813387
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

0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 35

Expert Comment

by:girionis
ID: 21816423
>//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

0
 
LVL 35

Accepted Solution

by:
girionis earned 350 total points
ID: 21816550
The other thing you could do is to change your programme a bit, to print to the network printer. So when it finds the printer on the network you simply print there.

> 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.

You shouldn't really need to do it. The PrintServiceLookup should return all the printers, even those who are on the network (assuming same network. If you want to use printers on a windows machien while you are using windows you should use samba).
for (int l = 0; l < services.length; l++) {

                   if(services[l].getName().substring(0,7).equalsIgnoreCase(<name of the printer on the network>)){

                                job.setPrintService(services[l]);

                            }

                        }

Open in new window

0
 

Author Comment

by:quicksystem
ID: 21819326
Thanks the solition was implemented.


Rgards

Mark
0
 
LVL 35

Expert Comment

by:girionis
ID: 21820177
Thanks for accepting, glad I was of help :)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now