I have a code here that opens a new window when i click the hyperlink.
Chapter 1<br/>
Chapter 2<br/>
How can we return a value to the servlet so that it knows which link has been accessed??
Ive made the pages for the two links i've made here. But i need the servlet to know which page has been accessed. Inside the servlet a function is calling a jexcel app. The return value is different for both pages. That is why i need a UNIQUE return value for each link.
The link visited state is stored in the browser, it is not a really good idea to rely on that! It will be gone after a cache clear for instance. You should maintain a DB of user activity, if you need this info on a user level.
Related
I am an android developer and I knew about GeckoView recently.
I can get source code of a web page by android WebView (java code).
However with my new website, android Webview can't load the webpage but GeckoView can.
Now I want to get source code of a web page by GeckoView.
Is there any body know the solution to resolve this problem ?
Thank in advance
As far as I know, you cannot use GeckoView to get the source code, but the geckoview library does have GeckoWebExecutor. Take a look at the fetch method and the WebResponse it returns.
By converting the WebResponse.body InputStream to a String you can get the source code.
One option for doing this could follow these steps:
detect which page my geckoview loaded;
save the loaded url into a sharedpref;
if user wants to see the source of the page, load the url saved within that sharedpref.
So...
(1-2) as you already know, geckoview doesn't have that handy shouldOverrideUrlLoading() method, so you will have to deal with the NavigationDelegate class, which has the onLocationChange(), where i put this line, which does nothing more than save the current url as a string into a sharedpref string named "geckoViewUrl":
sharedprefs.edit().putString("geckoViewUrl", url).apply();
having all setup before (sharedprefs etc). Detected the loaded page and saved the url into a sharedpref, let's go to the final step 3.
(3) for my use, wishing only to allow the user to see (and copy) the source, for the law of the minimum effort i used another activity with a plain and old webview to display it. This is very easy to implement and rises no confusion to the user. He/She wants to see the code, i show it in another activity. When it's done, he/she closes the new activity and life continues.
So, user wants source? Load another activity with a webview and make it load that saved sharedpref string:
addr2open = pref_out.getString("geckoViewUrl", "");
Doing this (for example), you get the url user wants to see source assigned to a string var. To finish, all you have to do is to make the webview load this string preceded by the precious word view-source:, this way:
webView.loadUrl("view-source:" + addr2open);
That's it. Of course you could implement a solution relying on the same activity, or using geckoView, showing multiple options, menus etc. I only wanted to show you a way to solve your problem in a nice 'n' easy way. You asked for "How to get a web page source code with GeckoView". Here is the answer. Works perfectly. If this is good for you, please, accept this as the correct answer. Thank you. Happy coding.
One example on the use of "view-source", from the creators of GeckoView: https://firefox-source-docs.mozilla.org/devtools-user/view_source/index.html (see item "Link to a line number").
This question already has answers here:
Design Patterns web based applications [closed]
(5 answers)
Closed 6 years ago.
EDIT: I have posted a somewhat shorter and revised question here: Java web development: transfer control from one servlet to another while passing the request object (Version 2)
As more or less a beginner at Java web development, I’m unsure about how I should structure the flow between servlets/pages when a form is submitted (POST). It’s an elementary issue, I suspect this may be an easy question to answer for the experts. (Still, my book and some googling didn’t deliver a clear answer.) My question is a bit long, and that's because I want to make it clear where I'm coming from. Thanks for you patience.
Let’s say we have two servlets A en B, with each having its ‘own’ .jsp-page; let’s call those pages a.jsp and b.jsp respectively. Now as long as there are no forms on either page (i.e., no POST method used), it’s clear how things should go. That is, before any .jsp-page is shown, the corresponding servlet is activated, doing some preparation for the .jsp-page by setting the relevant data elements (most notably, as attributes of the request object) that the .jsp-page needs, then forwarding the request object (etc.) to the .jsp-page, which then actually displays the page with the data. So for example, a link on page a.jsp may link to the servlet B, and on clicking that link a GET-request for servlet B is triggered, which then does some preparation (setting some request attributes), before forwarding to its ‘own’ .jsp-page (i.e. b.jsp).
But now let’s assume that page a.jsp displays a form with a submit button, method=”POST” and action=”B”. Then yes, servlet B is activated, and this servlet has to determine whether the data entered by the user is valid. If the data is in fact valid, we can simply forward to b.jsp, no problem there. But what if the data is NOT valid?
In that case, we obviously want to show a.jsp (the form page) again, with the data that the user entered the first time still present. One way to achieve this, is to simply have servlet B forward to a.jsp (thus bypassing servlet A). However, there is a big problem with that: the URL shown to the user, in the address bar, will still read “……/B”. So the user will see the correct page (i.e., a.jsp, containing the form), but with the wrong URL (/B). So for example, if we take “Register” and “ThanksForRegistering” instead of “A” and “B”, the user will see register.jsp – but with URL “……/ThanksForRegistering”! Not good.
And calling ‘include()’ instead of ‘forward()’ on the request-dispatcher doesn’t seem to work either. If we do that, not only does it result in a GET-request (as opposed to the POST-request we want), but we actually lose the whole (original) request-object with its attributes (which we need, after all, to re-populate the form). At least, that’s what my own experimentation seems to show. So using ‘include()’ doesn’t seem like a viable option at all.
Another obvious idea is to have "action=A" (instead of "action=B") for the submit. Then the servlet A itself can handle the validation, and if validation fails it can simply forward to a.jsp again, no problem. BUT then what if validation succeeds? Then we want to show the follow-up page b.jsp, but that page may well need the attributes from the original request-object (from the form-submit) again; for example, to have the user check that his entered data was in fact all correct. So basically we have the same problem as before, but with the roles of A and B (and their respective .jsp-pages) reversed. So this doesn't seem like a real solution either.
And I don’t see any other alternatives.
So basically, I’d simply like to be able have one servlet give control back to another servlet, but with the request object being passed from the former to the latter servlet. Or, if that’s not possible, I’d want to be able to forward from servlet B to a.jsp directly, but with the correct URL shown to the user. Or any other way to accomplish what I want.
Many thanks.
I think that the assumption that there has to be one page per servlet is causing the problem here....have one servlet which based on input redirects,forwards or includes a particular page....you dont really need to always invoke a different servlet for a page.....you can have a single front controller with a view resolver the combination of which will redirect or forward to a page.
You can use filters to achieve the same thing or think of setting attributes in HttpSession if validation is successful and retrieve the data in all the pages whenever it is required.
session.setAttribute("object", object);
I hope this is what you are looking for.
I have got a webpage with a few functions on it to load different content dynamically into the content section. What I want to achieve is by pressing back button to execute last/previous function. At the moment it goes to the previous page. Is it possible to do without hash tags in the URL bar as well? Seems like create a history of function list. Thanks
I suggest you to use the session.
Note that there is a plugin named JQuery session plugin, available from here
Tutorials links :
Lassosoft
weschools
Is there any Listener in Java, which can detect, if the content of the page has been changed? "changed" means text in the page has been added/removed...
Process: Author modifys the page and activate it. In publish Instance it must be checked if the page content has been modified/changed
I don't think there is such listener. You're gonna have to reload/access the page or you can hook it up so when the author submits his changes you insert a value to the database that this specific page has been modified. After that you just read the data from the DB using a timer that triggers every now and then and if new line appears you do your action.
This is more of a design question and you should think about what project you're working on and what's the best approach to implement this feature.
Apache sling can handle events. There is nice tutorial here http://sling.apache.org/documentation/tutorials-how-tos/how-to-manage-events-in-sling.html .
Basically create a listener ad check if the event relates to a page node (or its subnode). Then apply whatever logic you want.
Be careful to check whether you are in an author or publish instance ( or turn off the service in author)
There is this site wich in the address bar only shows like "http://example.com/examplepage.aspx".
Normally if it would have parameters behind it you probably could just copy that one.
But since it doesn't, how do i bookmark this page.
It doesn't necessarily have to be a bookmark, but at least an easy way to access the page.
(fyi I know basic HTML and Java, maybe it's only possible programmatically).
thnx
Generally dynamic pages (taking in context with the question) are not book mark friendly.
You could probably sniff the incoming request, and create a fake form which you can then submit later.
However there may be situations where there are parameters such as session id which are valid for only small periods of time.
You should read up on sessions. In really simple terms, a session is assigned to users accessing a website. They have an expiry period. IF you stay idle beyond set time (determined by the developer) you will not be able to get in. And every time you log back in, you may be assign a new session.
You would have noticed, that some websites automatically log you in, this is mostly done with the help of cookies. Cookies work in tandem with sessions, they store very basic information, so the next time you come back to a website, it will be able to identify you as a returning user and provide you with access.
Then again, some pages don't use sessions, they might have their own custom way of identifying users.
Bookmarks can be used in dynamic pages, if the code allows you to send GET requests, if they don't have any other extra parameters which will block you.
To Summarize:
Dynamic page not very bookmark friendly.
There may be parameters used to access a webpage which change constantly, which you cannot really save.
You may be able to get into dynamic pages using bookmarks, if they don't use any of the dynamically changing parameters.
Since you know Java, you should probably read up on JSPs/servlets to get an understanding of what happens behind the scenes in dynamic pages.
Hope this answers your questions.