Java print a template - java

The problem is pretty simple but I cant find a straight-forward solution online.
I have a document template, which I want to be able to populate with information (different information, more than once) and then print. My initial thought was to create a html template, and then send a parsed version to the print.
I suppose there is more than one question here:
What is the best approach to solve this problem?
If the HTML parsing is the best solution can someone point me in the correct direction as I dont know where to start.
Thanks.
-- EDIT: a better example to clear up any confusion.
On a java GUI program, the user is able to pull up a customers information. I wish them to be able to print an invoice at the click of a button. My current problem is deciding how to populate the invoice with the customers information and send it to the printer.

I would go for a web HTML form. Let the user populate the input fields and pass them in the POST body when a form submit button is pressed. Then you analyze the input and create the final document version (on the server) and only then return the document to the client with an appropriate MIME type in the response header (that's how you enable the printing).
For example (simple form with submit button)
<form name="input" action="html_form_action.asp" method="get">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>
EDIT:
For building document template take a look on playframework.

Related

Changes in browser are showing up in the server

Sorry for my bad English.
I have this little part of my code that gets a value from a Servlet and i show it.
<form method="post" action="rent">
<div class="card-body">
<h4 class="card-title">Office Number: ${o.id}</h4>
<input type="hidden" value="${o.id}" name="officenumber">
</div>
</form>
To be able to get that value and send it to another servlet i use an input with hidden type. Then, i catch it in my servlet
int officenumber = Integer.parseInt(request.getParameter("officenumber"));
RequestDispatcher rd;
request.setAttribute("officenumber ", officenumber );
rd = request.getRequestDispatcher("/rent.jsp");
rd.forward(request, response);
Then i show it in a new JSP. The problem is that everybody can use "Inspect Element" tool, change the value from my hidden input and the new inserted value will be sended.
Is it anyway to avoid that?. Thanks in advance.
that's why you should not rely on elements coming from the client.
there must be a verification done on server.
i suggest reading on putting the logged-in user's id on session,
and when receiving a request, you need to make sure the office-id belongs to the user, and the user has permissions to change/ delete it.
You should definitely store the "hidden input" on server side and should not let the user send that value to you. If you need the user to send the value for some reasons, you should authenticate the user (login, password, pin, or any method) and then validate the value that is given to you.
There is a possibility of XSS attack in such case. What you can do is provide a Encoding of text for parameter named officenumber at client side. Later decode that text at server-side.
You can apply rules from OWASP cheat sheet as per you requirement.
By doing this, Even though the value is changed at client side it will not harm/break your server side code.
Note: Anyone change the value of input parameters of a form before submitting it, So its a developer's responsibility to provide security to the code/Application.

Extracting and processing textarea value form without changing the page

I have a JSP file in which there are two textareas and a submit button all in a form ;
<form action="" method="post">
<textarea name="inConsole" id="in" cols="100" rows="10"> </textarea> <br/>
<input type="submit" name="Send Command"/> <br/>
<textarea name="outConsole" id="out" cols="100" rows="10"></textarea>
</form>
this page is supposed to work like any SQL program. so the user types a command in the first textarea and clicks submit, then the value of textarea will be extracted to a field and a method will take care of the command and return a log (1 row inserted, error:bad syntax etc) which will be displayed in the second textarea.
I know how for example make a login page and send data and redirect user to a new page(new jsp) file if user pass is correct.
what I can't find is how can I do all the things that I said above without going to a new page while using form action.
I have checked other questions that linked the action attribute to a servlet which was confusing for me( the way that a servlet was called). I'm looking forward to use a simple scriptlet for this purpose like the one I used for my login page:
<%
DatabaseLoginTest dbLogTest = new DatabaseLoginTest();
if (dbLogTest.DBLoginChecker(request.getParameter("user"), request.getParameter("pass")) == true){
%>
<p>Login Successful</p>
<% } else { %>
<p>Login Failed</p>
<% } %>
also I'm aware that adding java scripts(not Javascript scripts:) ) to html isn't a good practice(and the reasons for it) but I think this might be easier for a simple program that I'm working on.
p.s: I'm using tomcat and Intellij for developing this web application
and I have made a custom SQL so I only need the code that gives me the textarea value and the one that sets the other one's value
Update: now I know I should use javascript but I don't know how can I send the data extracted by javascript to a java method.
If you want to do this while remaining in the same page, you have to use Javascript. This is because if you want the server to be able to re-render the page, there has to be a page refresh.
You would need to write onClick handler for the submit button and make a Ajax call to your server to a specific URL with the user input. This URL would serve the data needed for the necessary UI changes.
You can use a scriptlet to generate the HTML that would be shown in the webpage but this would only suffice for a simple use-case and it would be a lot simpler if, say, your service returned just the data required to make the UI change and actual UI change is handled by the JS.
Also,I don't think it is a bad practice to embed JS in HTML. Sure, you can optimize this by including a JS source file but that's a separate optimization.

Not saving texts when selecting other option from dropdown list in HTML form

I am using the following code to develop a dropdown list in a html form.
<html:select property="complaintCategory" onchange="showfield(this.options[this.selectedIndex].value)" >
<html:option value="option1">option1</html:option>
<html:option value="option2">option2</html:option>
<html:option value="option3">option3</html:option>
<html:option value="Other">Other</html:option>
</html:select>
<div id="div1"></div>
I want to create a text box so that user can write something when they select Other option. The function showField(name) is creating the new text box.
function showfield(name)
{
if(name=='Other')document.getElementById('div1').innerHTML='<input type="text" name="complaintCategory" />';
else document.getElementById('div1').innerHTML='';
}
The problem I am facing is that when I select "Other" option from the dropdown list and then write something on it, it is not saving the texts, it is saved as value Other only. I want to pass the texts written in the text box as the complaintCategory. Would really appreciate someone's help on this, i am stuck.
Thanks in advance...
(I'm working on the assumption that <html:select property="complaintCategory" will output <select name="complaintCategory". It is usually helpful to narrow down the problem to server side code or client side code and provide only one of them.)
When you submit the form (after selecting other) you will have 2 elements with the same name.
Whatever server side code you are using to read the submitted form data, it appears to be taking the first value.
Either:
Modify your server side code to get the second value (I don't know the API you are using to parse the form data so I can't tell you the specifics for that)
Call the generated input by a different name and an if statement.
e.g.
client side:
if(name=='Other')document.getElementById('div1').innerHTML='<input type="text" name="otherComplaintCategory" />';
server side (psuedo code):
IF complaintCategory is "Other":
THEN complaintCategory = otherComplaintCategory

sending request to a page

I'm trying to fill-out a form automatically and press a button on that form and wait for a response. How do I go about doing this?
To be more particular, I have a a --HUGE-- collection DNA strains which I need to compare to each-other. Luckily, there's a website that does exactly what I need.
Basically, I type-in 2 different sequences of DNA and click the "Align Sequences" button and get a result (the calculation of the score is not relevant).
Is there a way to make a Java program that will automatically insert the input, "click" the button and read the response from this website?
Thanks!
You can use the apache http client to send a request to a web site.
Look at the source to the page in question, and you'll find the part. This contains all the fields that need to be sent to the server. In particular, you'll see that it needs to be sent as a Post, rather than the more common Get. The link above shows you how to do a post with the http client code.
You'll need to provide a nameValuePair for every field in the form, such as these ones:
<input type="hidden" name="rm" value="lalign_x"/>
<input type="checkbox" name="show_ident" value="1" />
<textarea name="query" rows="6" cols="60">
It will probably take some trial and error for you to get all the fields set up correctly. I'd recommend doing this with small data sets. Once it all seems to be working, then try it with your bigger data.
In Python you can use mechanize library (http://wwwsearch.sourceforge.net/mechanize/). It's quite simple and you doesn't need to know Python very well to use it.
Simple example (filling login form):
br = Browser()
br.open(login_link)
br.select_form(name="login")
br["email"] = "email#server.com"
br["pass"] = "password"
br.submit()
You could probably do this using Selenium.

How to send data to ActionBean via JavaScript?

I'm playing with Stripes and I wonder if it is possible to send Javascript generated data to ActionBean. To be more specific, when I click with my mouse on certain element on page, I want to send ID of that element back to ActionBean after clicking on stripes:link. Providing I already have that ID saved in a JS variable id, how do I do that?
Are you using parameterised link?
<stripes:link id="mylink" href/beanclass="..." event="...">
<stripes:param name="id" value="some_default_value"/>
Click on me!
</stripes:link>
Which would most probably generate: http://mysite.com/...?id=some_default_value, which you would later use javascript to change some_default_value to the id you want?
Note: Suggestion unverified. I've no dev tool installed on this old lappie.
Edit: On second thought, why not just write some javascript to append "?id=" + id; to the link's url address?
create a hidden input element in your html
<input type="hidden" name="?" id="?" />
use javascript to set the value of it
document.getElementById("?").value = ??;
and the value will be posted with your form submission.

Categories