Mail form in Play Framework 2.0.X - java

I'm using Play Framework (2.0.4) and I wonder what is the best way to create a page with a form for sending an email. I know that there is a plugin for sending email, so this isn't a problem - I can write the controller with a method which sends the email.
My question is more about the action I should provide (in routes file). Should I create a POST action which takes for arguments (sender name, sender email, subject, body)? or should I somehow create a model object which would be filled in the form and pass to the action in the controller? What is the best practice? And how to glue it properly (how should the action look in the routes file, how should the view look like)?

You need two views - one with form (let's call it mailForm), second - with body (bodyHtml) of the mail. (optionally you can create bodyTxt if you want to send HTML and TXT version.
Dedicated model will be good helper, as it will help you use the Play's Form<T>, also if required you will be able to store sent messages in DB. Anyway you can also operate on the map of Strings - especially if you plan to make many dynamic forms (with unknown number of fields).
After filling the form it will go to for an example sendEmail() action, where you need to fill the Form (bindFromRequest) eventually create the object and save to DB and finally pass to bodyHtml view as argument. Of course instead of returning rendered view as a action's result you should use it with toString - to send it with mailer. The action should return a redirect to some 'Thank you' page. and that's all.
Note: Written fast, if something will be unclear, let me know ...

Related

Send a bunch of values from jsp to java code

I have a form on my jsp with fields - Name, IP, username, token.
It is mapped to a table with same column names. I have created a Bean class too.
Now, I am not sure what is an ideal way to send all the parameters from jsp to the java code.
I know few options like -
Create input with type hidden and then set its value in the jsp.
<input type="hidden" id="name" name="name"/>
and then retrieve its value in the code using request.getParameter("name")
Somehow I dont feel this is an ideal way.
Create a JSON with all the values of the input boxes and set the json file as one input and read it on the java code using org.json;
Which one of the two is the better way to do it? Is there any simpler, effective and much better way?
Everything which is built using HTTP methods must send values to the server in one of three places:
Parameters in the url itself (GET requests)
Content in the body of the request (POST requests)
Key-value pairs in the header of the request (HTTP method agnostic)
Any mechanism you use to send a value back to the server (excluding web sockets), no matter what the framework, will use one of these mechanisms underneath.
That being said, use whatever best meets the requirements of your application:
Form-based, GET requests:
are simple to understand
don't require much overhead either on the front-end or back-end
are easy to test (just access the url using the appropriate query string)
Form-based, POST requests:
are also simple to understand
also don't require much overhead either on the front-end or back-end
are not as easy to test (you can't just access the url using the appropriate query string)
Ajax-y, JSON body, POST requests:
are the new hotness
require a bit more front-end work (creating the request in JS and sending it)
don't require the browser to make a full-page request/response

How to add a flag to existing POST api

I have a REST api built for creating orders. The behavior is such that the person who creates an order received an email back saying "You created an order XXX". This email is triggered all the time.
The api appears like this
http://api.mytestevnt.com/ordering/orders - POST with request body as the order entity json.
Now i want to give a feature to the api caller to indicate if the email notification is necessary or not. What's the best way to do this?
I think it depends on whether email notification is data or metadata. If it's part of the order, then definitely add it to the request body. If it's metadata, you have two choices. If you think there will be lots of metadata, you can either edit the order to have a metadata section or you can POST the metadata separately. If there will only be a limited amount of metadata, I would suggest using a query parameter.
You should avoid using a header unless you control the entire path from the client to the server, because proxies or load balancers are allowed to strip non-standard headers.
Include in the POST body a send_email=1 or send_email=0 param. You'll extract that, and see what the user wants to do.
Search "how to get POST variables in JAVA".
Accessing post variables using Java Servlets
You can do like this:
Add a new Java attribute(like boolean emailEnabled) in your Java Request Object for your REST service.
Client side which invokes your REST service need to provide that parameter you added in your server side, you can set a default value for that too.

java - parsing an aspx website - post parameters

I have my client's e-shop, which is created by another company. I want to parse all the products and put them in an xml. I know how to get to the first page of each "brand" but I have difficulties passing the argument to change the page for the paginated results.
This is the e-shop "http://www.gialia.net.gr/ProductCatalog/20/CAR.aspx" that points to one brand.
When I user tamper-data on firefox I see that when you want to press the second-page of the results is posts the :
"__EVENTTARGET=ctl00%24wpmMain%24wp131820866%24wp512420601%24dpgTop%24ctl01%24ctl01"
the last string: "ct101" means go to page 2, If I change it to ct102 it goes to page 3 etc.
BUT i'm trying to create it as a GET request so I can create these parameters dynamically in my Java code and parse each responce. But when I create the url as:
http://www.gialia.net.gr/ProductCatalog/20/CAR.aspx?__EVENTTARGET=ctl00$wpmMain$wp131820866$wp512420601$dpgTop$ctl01$ctl02
I get no results.
Can someone please take a look and give me some suggestions?
The site you give us here is very poor in design concerning the search engines (SEO), and so the parse of the page one by one is too difficult.
To change page is make post back, and with javascript only. So you must do the same to move to the next page of the catalog, you need to make a full post back of the page with all the parameters.
Now, the page is so bad designed that the programmer have disable the __EVENTVALIDATION of the controls probably because he not let him do wrong things, so when you can tamper the data, but still you need to make post back. By simple type on the url one only parametre the code behind did not understand that is post back. You need to send and at least the Viewstate and the rest hidden parameters.
But isn't more easy to just get from your client access direct to the database and reads them from there ?

Java EE - creating a register-by-email setup

Assuming I have a web app (Java EE 6), I want to realize the following use case:
On a generic "register" page, user enters his/her email and submits
System sends email to user. In this mail, there should be a link to a full registration page
User follows link, fills out required credentials, and submits
System stores user in database, and sends confirmation email
All temporary data is discarded.
The part I am having problems with is mostly step 2. What I need to know is the following:
What is the best way of setting up a personal registration page for the user? Should I generate a temporary page and link to it? Should I give the user a special cookie? In either case, how can I implement this? No code is needed, I just need some pointers on where to start looking and coding.
EDIT:
A very important question here, no matter how I do it, is the following: How can I generate and deploy a temporary webpage? I am rather new to Java EE, so forgive me if this has an obvious answer.
If the registration page is standard for every user then there is no need in creating a temporary page and linking to it or creating a special cookie etc... Just create the page with the registration form on it and send your clients a link to that.
If you want a special customized registration page for a specific user, then you can just send an e-mail with a link to your client with their information as a query parameter (ie http://yoursite.com/registration?fname=jordan&lname=denison) or you could use something like PrettyFaces to make the URL more readable as well as making it easier to extract those parameters and fire a method with them.
If I had to do it, then I would have
Created a generic registration page
Created a unique ID for the user
Send the link to user which contains the unique ID as a parameter
The registration page throws error if the unique ID is not presented to it, or the ID is not valid (already used, or not issued)

Java Web Application

I am interested in creating a simple web application that will take in user input, convert it to an XML file and send the file to a database.
Coding wise I feel I am okay, it is just the general setup and what implementation to use I am a bit unsure of.
At the moment I have a JSP page containing a form, the user fills out the form and on submit a POST method is sent to a servlet, in the servlet doPost() method the servlet is instantiating a java object and passing it the user inputted data. The java object then writes that data to an XML file and sends it to the database via REST.
All I would be interested to know is if this the standard/optimal way of creating such a web application.
Any and all feedback is appreciated.
Thanks
For a "simple webapplication" this high level approach looks fine in general. However, if you want more critical feedback, you'd need to give more details about the low-level approach. It may for example happen that it isn't memory efficient and thus may break when the webapp is been used by over 10 users concurrently, just to give an example.
I only question the choice for the GET method. You'd normally only use it to retrieve data (SELECT), not to create/alter data (INSERT/UPDATE/DELETE). For that you'd normally use POST, so that no one can execute it "accidently" by just clicking a (bookmarked) link. Changing GET to POST isn't that hard, add method="post" to the <form> element and rename doGet() to doPost().

Categories

Resources