Input in html to java file - java

I need an input in my html file (I think via javascript) and I need to parse in my Java file.
To get user input in html, it's easy, but I don't know how to parse in my java file.
Any suggestions or tutorials? Thank you!

You can use jsoup like this.
String url = "https://stackoverflow.com/questions/50083471/input-in-html-to-java-file";
Document doc = Jsoup.parse(new URL(url), 3000);
System.out.println("title: " + doc.select("title").text());
System.out.println("users:");
for (Element e : doc.select("div.user-details a"))
System.out.println(" " + e.text());
This code parse this page and print title and user names.
result:
title: Input in html to java file - Stack Overflow
users:
l1den
truekiller
Ethan Three
saka1029

Ceate a form in html and when you submit call a servlet to handle the request. In servlet receive the value you want to handle by using request.getParameter(name) . There are many framework for web MVC like struts2 , spring MVC .

Yes, there are way many way to do it.
The simplest apporach is using html form, your html should include javascript that is able to submit a form (the data from user) to server (POST to an url) as a request.
and then server should program to response the request (the url you submit to).
and then you pickup a framework (serverlet is simple one, but if you have an existing project, check how it process form data).

Related

Jsoup with a plugin

I'm using Jsoup to scrape some online data from different stores, but I'm having trouble figuring out how to programmatically replicate what I do as a user. To get the data manually (after logging in), a user must select a store from a tree that pops up.
As best I can tell, the tree is not hard-coded into the site but is built interactively when your computer interacts with the server. When you look for the table in "view page source," there are no entries. When I inspect the tree, I do see the HTML and it seems to come from the "FancyTree" plugin.
As best as I can tell from tracking my activity on Developer Tools -- Network, the next step is a "GET" request which doesn't change the URL, so I'm not sure how my store selection is being transferred.
Any advice on how to get Jsoup or Java generally to programmatically interact with this table would be extremely helpful, thank you!
Jsoup can only parse the original source file, not the DOM. In order to parse the DOM, you'll need to render the page with something like HtmlUnit. Then you can parse the html content with Jsoup.
// load page using HTML Unit and fire scripts
WebClient webClient = new WebClient();
HtmlPage myPage = webClient.getPage(myURL);
// convert page to generated HTML and convert to document
doc = Jsoup.parse(myPage.asXml());
// do something with html content
System.out.println(doc.html());
// clean up resources
webClient.close();
See Parsing Javascript Generated Page with Jsoup.

Jsoup not getting full html

I am trying to Jsoup to parse the html from the URL http://www.threadflip.com/shop/search/john%20hardy
Jsoup looks to only get the data from the line
<![CDATA[ window.gon= ..............
Does anyone know why this would be?
Document doc = Jsoup.connect("http://www.threadflip.com/shop/search/john%20hardy").get();
The site you try to parse loads most of its contents async via AJAX calls. JSoup does not interpret Javascript and therefore does not act like a browser. It seems that the store is filled by calling their api:
http://www.threadflip.com/api/v3/items?attribution%5Bapp%5D=web&item_collection_id=&q=john+hardy&page=1&page_size=30
So maybe you need to directly load the API Url in order to read the stuff you want. Note that the response is JSON, not HTML, so the JSoup html parser is of not much help here. But there is great JSON libraries available. I use JSON-Simple.
Alternatively, you may switch to Selenium webdriver, which actually remote controls a real browser. This should have no trouble accessing all items from the page.

HtmlUnit Page response (Downloading File)

So im trying to use HtmlUnit to go to a URL but once you visit that url it downloads a json file regarding the data you want. Not sure how to word this but basically in HtmlUnit how can I get the result from a downloaded file.
I suck at explaining here look
trying to check user availability by this
private static final String URL = "https://twitter.com/users/username_available?username=";
...
HtmlPage page = webClient.getPage(URL + users[finalUsersIndex]);
so that basically creates a new page for each username thing is the URL + username returns a json file of user availability. I know how to read the json file but the problem is this
java.lang.ClassCastException: com.gargoylesoftware.htmlunit.UnexpectedPage
cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage
I get that on this line
HtmlPage page = webClient.getPage(URL + users[finalUsersIndex]);
I suppose I need to create a new page for the response but how would I do that since it automatically downloads file instead of per se, clicking a button which downloads the file. (Correct me if im wrong)
Sorry 4AM
As its name indicates, an HtmlPage is a page containing HTML. JSON is not HTML.
As the documentation indicates;
The DefaultPageCreator will create a Page depending on the content type of the HTTP response, basically HtmlPage for HTML content, XmlPage for XML content, TextPage for other text content and UnexpectedPage for anything else.
(emphasis mine).
So, as the exception you're getting indicates, the behavior you observed is the documented behavior: you're getting a page that is neither HTML, nor XML, nor text, so you get an UnexpectedPage.
Your code should thus be:
UnexpectedPage page = webClient.getPage(URL + users[finalUsersIndex]);

Return an Excel file from Java Servlet along with HTML content

Using java servlet i need to do a back-end DB query and populate the results in an excel file and provide to client user.
I have a working code of downloading Excel by setting the HtmlServletResponse object's contentType and Header like below:
response.setContentType("application/vnd.ms-excel; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename="+ "Report" + ".xls");
But my question is in addition to providing this Excel sheet as a download i also need to send the 'initial search criteria' selected by the user as a HTML. And for html, i need to set the content type like below.
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
I think it's not possible to set the content type to 2 different values. How to solve this problem?
To phrase the problem in a different way -- "I have a HTML search form based on which user selects a search criteria - While providing the results in an excel save as file, i need to repopulate the same html so that search criteria selected by user is not lost".
I am new to Servlets and not sure if this is very straight forward. Thanks for the help.
You can not respond to the Service Request with more than one response. You'd need to show the search page with the search criterea and have the search page create the excel-file (in an iframe or something).
You can try to save your search criteria in cookies.
Here is some info about it http://www.journaldev.com/1907/java-servlet-session-management-tutorial-with-examples-of-cookies-httpsession-and-url-rewriting

How do I change the text of a paragraph using servlet?

I have a paragraph with id = story and I want to change its text dynamically using a servlet. How do I do this? I'm new and using getWriter().println() seems to create a new document instead of appending to the existing one.
Thanks
Simple answer - you can't.
Server-side code cannot change a response which has already been sent to the client.
To change text inside an HTML tag, use Javascript on the browser.
See http://www.w3schools.com/ajax/

Categories

Resources