Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp does not send text file content to printer  RSS feed

 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Paul!

There could be any number of things wrong. Let's start at the beginning: Does that file actually exist on the server, and does Tomcat have access to it? And are there any error messages in the Tomcat log? Also, I hope that code doesn't try to display a dialog box, because there normally wouldn't be anybody sitting by the server to respond to it.

You might also consider adding some code which lets you know if the service variable is null (at line 21) rather than just doing nothing.

I also don't understand why there is code there to sleep for 10 seconds; what's the point in delaying the response to the user?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is the point of putting this code in a JSP at all?
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul @Bear
There are no errors in the tomcat logs and the file does exist. I have a store management web app and inside there is a place where i create a text file containing information relating to a given transaction. One would call it receipt information. I want to automatically send this info to the printer when a user clicks on a link.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, looking at your code I notice this:



Looks to me like you're opening a dialog box on your server. And like I said, that isn't going to work. At least you should work on an alternative which just prints without having to ask somebody something. But also you shouldn't have this code in a JSP. If it's really something you want to run on your server then it should be in a servlet. JSPs are for generating HTML, not for doing things like this. Scriptlets in JSP have been outdated for over a decade now, you shouldn't be using them any more.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul
I am using tomcat 8 and i think it converts the JSP pages into Servlets before running them. How do you think i could implement this?
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:I am using tomcat 8 and i think it converts the JSP pages into Servlets before running them.


This is a typical statement from people who have received no training in web application design. What Tomcat does internally with its JSPs is completely irrelevant to how you should design your applications.

As for getting your application to print a file which is hard-coded, if that's really your design... first of all get something working in ordinary Java code. It has to be something which has no user interface, remember. When you have something working then take that code and put it into the doGet() method of a servlet which is going to handle the print request.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's take a step back: do you want to print this on a printer that the *user* has access to, or a printer that the *server* has access to?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which leads me again to the question: why a JSP?

If it's because you think it will allow you to print something out on the user's printer, think again, and read this article to understand how JSP works and why it will won't have any access to any printers other than those on the server.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear
Thanks Bear. I now know that what i intended doing is just impossible. I had added a javascript code(see below) at the end of the html page that is to be sent to the printer at the user side. Is that a good thing to do?
<script>
window.print();
</script>
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good in what sense? From the brief snippet it looks like this code would be run no matter what - if so, I think that's unusual, and I would personally find it annoying. The usual approach is to put a print button on the page which when clicked calls such code.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ulf
That is exactly how i did it in my web application. As i do not have a physical printer, i use a pdf printer software that captures my print jobs.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way for server code (JSPs or servlets or whatever) to cause something to be printed on the client's printer without the user's approval. Anything you do will at best require the user to deal with the usual printer dialog. Which, as Ulf said, could be annoying to the user if that wasn't what they wanted. In general trying to force the user to do something is bad practice for a web application.

And once you get this working, it might be useful to apply CSS to hide parts of the page from appearing in printed versions. For example you might not want the "Print" button to appear on the printed page.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more general comment about designing web applications: you should generally act as if the application server is going to be in a dark room full of servers which is in a building thousands of kilometres away, and as if the client is using some computer which you know nothing about except that it might be an old piece of junk with an outdated browser. Those things may not be true but you need to keep the server and client parts separate, so it helps to act as if they are true.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!