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
376 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 35

Expert Comment

by:girionis
Comment Utility
>//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
Comment Utility
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
Comment Utility
Thanks the solition was implemented.


Rgards

Mark
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Thanks for accepting, glad I was of help :)
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

763 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

7 Experts available now in Live!

Get 1:1 Help Now