request.getParameter not working - java

I have developed a file download program, which is implemented by Java Servlet.
The link sent to the user is in the following format:
http://examples.com/file?id=6565_1_1_1_201110
When user clicks this link, it will send a request to a servlet program to find the file name and then download the file. In the servlet I use the following code to retrieve the id value:
String param = "";
synchronized (this) {
param = request.getParameter("id");
}
Now the problem is, some users can get the correct value, which is 6565_1_1_1_201110. However, some users have obtained the wrong value, like 656543_1_1_1_20111067, etc, which is not fixed. I do not know where are those additional number from?
Any idea on this? Thanks.

Related

Is there a way to retrieve file from SharePoint without file name?

I know how to retrieve a file with a specified name like this:
HttpGet httpGet = new HttpGet(siteUrl +
"_api/web/GetFolderByServerRelativeUrl('Shared%20Documents/Working%20Files/FolderName')/Files('"
+workbookName+"')/$value");
but is there a way to retrieve it without the name? maybe by index?
Ive checked the documentation on https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest but saw no other examples on how else to retrieve files
Every file you upload into the Sharepoint will have the field Id mapped to it. This ID would be returned as part of the response whenever you create the file in the Sharepoint. This response would also return several links and fields related to the particular file you uploaded, in order to perform any operations on that file.
Using these fields such as Id, E-Tag you should be able to form another request to get the file name.
You can also find the request URL in the response you received after file creation.

How to run a jsp file by different url pattern other than filename.jsp?

I want to create a blogging like CMS in jsp.
So I have a "post.jsp" which will dynamically display the content.
I have a database which stores some url pattern and their content.
For example database stores:
url: www.example.com/abc
content: hello
url: www.example.com/xyz
content: world
I want to do that when a user will enter "www.example.com/abc" it will run "post.jsp" and check in database whether the url is present or not. If present then it will display "hello" else 404 error/
Similarly if user will enter "www.example.com/xyz" it will again run "post.jsp" and if the url present then it will display "world" else 404 error.
Every time the url should run "post.jsp" if no extension like html or jsp is present in the url pattern. If a url "www.example.com/contact.jsp" is entered by user then it should run contact.jsp without entering "post.jsp" because the url pattern contains a .jsp extension.
Hope you understand my requirement. How to do this?
I would create a mapping for 404 in my web.xml and define a JSP / servlet to be called whenever a URL is called that is not found. In that Java code I would do something similar to this (pseudo code !):
String request = getRequestUrl();
if( !request.endsWithIgnoreCase( ".jsp" ) && !request.endsWithIgnoreCase( ".htm") {
String tagToSearch = request.getEverythingAfterSlash();
String content = Db.searchFor( tagToSearch );
Response.write( content );
}
And please make sure that no-one is able to inject dangerous SQL by using this mechanism !
How this needs to be implemented depends quite a bit on which environment you use (Java version, Servlet version, Framework, Application server)

Upload files using selenium

How to upload files from local via window prompt using selenium webdriver?
I want to perform the following actions:
click on 'Browse' option on the window
from the window prompt go to the particular location in the local where the file is kept
select the file and click on 'Open' to upload the file.
Have you tried using input() on proper file input control?
WebElement fileInput = driver.findElement(By.id("some id"));
fileInput.sendKeys("C:/path/to/file.extension");
I have used below three different ways to upload a file in selenium webdriver.
First simple case of just finding the element and typing the absolute path of the document into it. But we need to make sure the HTML field is of input type. Ex:<input type="file" name="uploadsubmit">
Here is the simple code:
WebElement element = driver.findElement(By.name("uploadsubmit"));
element.sendKeys("D:/file.txt");
driver.findElement(By.name("uploadSubmit"));
String validateText = driver.findElement(By.id("message")).getText();
Assert.assertEquals("File uploaded successfully", validateText);
Second case is uploading using Robot class which is used to (generate native system input events) take the control of mouse and keyboard.
The the other option is to use 'AutoIt' (open source tool).
You can find the above three examples : - File Uploads with Selenium Webdriver
Selenium Webdriver doesn't really support this. Interacting with non-browser windows (such as native file upload dialogs and basic auth dialogs) has been a topic of much discussion on the WebDriver discussion board, but there has been little to no progress on the subject.
I have, in the past, been able to work around this by capturing the underlying request with a tool such as Fiddler2, and then just sending the request with the specified file attached as a byte blob.
If you need cookies from an authenticated session, WebDriver.magage().getCookies() should help you in that aspect.
edit: I have code for this somewhere that worked, I'll see if I can get ahold of something that you can use.
public RosterPage UploadRosterFile(String filePath){
Face().Log("Importing Roster...");
LoginRequest login = new LoginRequest();
login.username = Prefs.EmailLogin;
login.password = Prefs.PasswordLogin;
login.rememberMe = false;
login.forward = "";
login.schoolId = "";
//Set up request data
String url = "http://www.foo.bar.com" + "/ManageRoster/UploadRoster";
String javaScript = "return $('#seasons li.selected') .attr('data-season-id');";
String seasonId = (String)((IJavaScriptExecutor)Driver().GetBaseDriver()).ExecuteScript(javaScript);
javaScript = "return Foo.Bar.data.selectedTeamId;";
String teamId = (String)((IJavaScriptExecutor)Driver().GetBaseDriver()).ExecuteScript(javaScript);
//Send Request and parse the response into the new Driver URL
MultipartForm form = new MultipartForm(url);
form.SetField("teamId", teamId);
form.SetField("seasonId", seasonId);
form.SendFile(filePath,LoginRequest.sendLoginRequest(login));
String response = form.ResponseText.ToString();
String newURL = StaticBaseTestObjs.RemoveStringSubString("http://www.foo.bar.com" + response.Split('"')[1].Split('"')[0],"amp;");
Face().Log("Navigating to URL: "+ newURL);
Driver().GoTo(new Uri(newURL));
return this;
}
Where MultiPartForm is:
MultiPartForm
And LoginRequest/Response:
LoginRequest
LoginResponse
The code above is in C#, but there are equivalent base classes in Java that will do what you need them to do to mimic this functionality.
The most important part of all of that code is the MultiPartForm.SendFile method, which is where the magic happens.

How to request a file and get it or get a message in case it cannot be created from the server

Using JQuery and Spring's #ModelAndView annotation for the controller.
I'm trying to code a process in which the user clicks an icon and if a certain criteria on the DB is met, a zip file will be produced on the server containing a bunch of files, then this zip file should be sent to the browser for saving.
If the criteria isn't met, then an error message should be sent to the browser telling there isn't any file to be created and produced.
However if I use JQuery' .post method, I can receive the error message (if that is the case) but never the zip binary file.
If I use a regular Href Link I can receive the file (if that is the case) but don't know how to receive the message when the file cannot be produced.
Is there an alternative or a standard way to do this?
Thanks for your support!
-Gabriel.
You should probably split your server-side method in two:
the first one validates the criteria. If unsuccessful, it notifies of an exception, otherwise it returns a URL to the method in next point
the second one actually returns the zip file
In your frontend, the code will look something like this:
$.post(urlToPoint1, data, function(response) {
if (response.success) {
// download the file using the url provided
// (pointing to method described in point 2)
window.location.href = response.url;
}
else {
alert('whatever');
}
});

Send Mulitpart file from Controller to JSP (Spring)

My requirement is to upload a JPEG/PDF file & save it as BLOB.
We have done it. But if server side error occurs which redirects to JSP page, I will get all data (e.g. input fields, drop downs, checkbox, etc.) except file.
Then again I need to choose a file.
Is there any way to preserve a file or send a file from controller to JSP.
No it is not possible.
The simplest workaround is to keep the uploaded file in session so that you can recover it during the next form submission. Take care to users working with several tabs/windows: use a session key that clearly identifies the form on which the user is working. You could for example generate a unique identifier that you then store in a hidden field of the form.
To be able to download it again, you would need to provide second mapping that retrieves the file from the session.
MultipartFile inputFile = fileUploadBean.getFile();
HttpSession session = request.getSession();
if(!(inputFile.isEmpty())) {
session.setAttribute("inputFile", inputFile);
}
logger.info("inputFile : " + session.getAttribute("inputFile"));
if(inputFile.isEmpty() && session.getAttribute("inputFile")!=null) {
inputFile = (MultipartFile)session.getAttribute("inputFile");
}
This is what I did.

Categories