Firefox rewrites GET request - java

Background: I have a Java webserver class (NanoHTTPD) which my application extends by adding sessions, page templates, authentication, and dynamic content. I wanted to make the application self contained and not rely on anything but SQLite. It is meant to run with only Java and SQLite installed.
Now for my problem. I am testing a simple index page with only Logout link. If a user goes to the index without logging in, they are HTTP redirected to the Login page. When they POST the credentials and are validated, it returns them to the index page. If you click the Logout link in Firefox, my application only receives a URI to the index('/'). Following the same process in Chrome it logs the user out as expected. I do no modify the GET URL at any point; I only run uri.equals("/logout/"). Is there any reason why Firefox would interpret this link differently?
Login HTML:
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<div>
<p></p>
<form method="post" action="/login/">
<table>
<tr>
<td>
<label for="username">Username</label>
<input type="text" name="username" id="username" maxlength="100" />
</td>
<td>
<label for="password">Password</label>
<input type="password" name="password" id="password" />
</td>
<td></td>
<td>
<input type="submit" value="Sign In" />
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
Index HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Index</h1>
Logout
</body>
</html>
The code for matching URL's is just .equals() and the only code running before this is NanoHTTPD which is located here: https://github.com/elonen/nanohttpd/blob/master/NanoHTTPD.java
One more thing, I have made a page, "test.html", to which I copied the source from above. Firefox does not handle it correctly either and stays on the page without giving a File Not Found error. Additionally, if I change the page to use "/logout1/", everything works.

Related

How to retrieve text box value from a JSP page [duplicate]

This question already has answers here:
How can I upload files to a server using JSP/Servlet?
(14 answers)
Closed 7 years ago.
I am creating a web application in Java and am having trouble retrieving a value from a text box. My aim is to ask the user to enter their email address and then use the value entered for the rest of my application. I do this by attempting to pass the value as part of the URL in the jsp file (and retrieve it using request.getParameter()).
However, the value that I keep retrieving is null.
Here is my code:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>Display file upload form to the user</TITLE>
</HEAD>
<center>
<label for="email">Enter email address</label>
<input id="email" name="email">
</center>
<% String mail = request.getParameter("email");
System.out.println(mail);%>
<BODY>
<FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp?e=<%=mail%>" METHOD=POST>
<br> <br> <br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<td colspan="2" align="center"><B>UPLOAD THE FILE</B>
<center></td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td><b>Choose the WebEx File To Upload and Convert:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit"
value="Upload and Convert Recording"></td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>
I am trying to pass the mail value into the jsp file so that I can use it in my application
Put the following code between form tag.
<form ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<center>
<label for="email">Enter email address</label>
<input id="email" name="email">
</center>
-------
-------
</form>
on Server side write
String email=request.getParameter("email");
this will definitely solved your problem.
you are requesting for upload.jsp?e=<%=mail%> where the parameter name is e and an other side you are getting the request.getParameter("email");
try this request.getParameter("e");or use this instead upload.jsp?email=<%=mail%> for request.getParameter("email");
you can try :
<form .... ACTION="upload.jsp?e=${mail}" method="POST">
And send parameters :
request.setAttribute("mail", "mymail#abc.com");
As per my understanding you are sending both parameters and file to the server by specifying multipart/form-data as form encryption type. As per my knowledge in this mode, normal parameters except files are not available directly on server side.
If you can use third party libraries in your application, you can use Apche Commons and Commons IO libraries. You can also use the example specified here commons file uploading to overcome your problem.
You can visit multipart/form-data structure for more details.

Creating web dynamic project with Eclipse EE IDE

I am a newbie trying to create a simple web application using JSP and Mysql.
I am using following thing:
Eclipse EE IDE
Tomcat 7.0
Dtabase: MySQL
MySQL Connector
In my page I have row user add row dynamically that s work fine. I want my values should get stored in database after submit.I have searched lot on net but not find anything which can solve problem.I am giving code of current page.I know have to do connection also however I tried but not succeeded.
Thanks in advance.
JSP & Javascript:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#add").click(function() {
$('#mytable tbody>tr:last').clone(true).insertAfter('#mytable tbody>tr:last');
$('#mytable tbody>tr:last #name').val('');
$("#mytable tbody>tr:last").each(function() {this.reset();});
return false;
});
});
</script>
</head>
<body>
<form>
<a id="add" href="javascript:void(0)">Add another Credit card</a>
<table id="mytable" width="300" border="1" cellspacing="0" cellpadding="2">
<tbody>
<tr class="person">
<td><input type="text" name="name" id="name" /></td>
<td><input type="button" value="name" /></td>
<td><select>
<option>value1</option>
<option>value2</option></select>
<td><input type="text" name="name" id="name" /></td>
</tr>
</tbody>
</table>
<input type="submit" value="submit">
</form>
Logout
</body>
</html>

JSP requestDispacthing

I have two JSP pages. In first page I have given fields to fill personal details and I have written request.getRequestDispatcher("second.jsp") and forwarded the the request. But When I run the "first.jsp" on server in eclipse, it is directly going to "second.jsp" but in URL it is shopwing "first.jsp". What might be the problem?
First.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<br>
<h2>Enter Your Personal Details</h2>
<form action="personal.jsp" method="get">
<table>
<tr><td>Name: </td><td> <input type="text" name="name" /><br /><br /></td></tr>
<tr><td>Email-ID: </td><td><input type="text" name="email" /><br /><br /></td></tr>
<tr><td>Date Of Birth:</td><td><input type="text" name="dob" /><br /><br /></td></tr>
<tr><td>Password: </td><td><input type="text" name="pass" /><br /><br /></td></tr>
<tr><td>Age: </td><td><input type="text" name="age" /><br /><br /></td></tr>
<tr><td><input type="submit" /></td></tr>
</table>
</form>
<%!
String uname=null,pass=null,email=null;
String age=null,dob = null;
%>
<%
uname= request.getParameter("name");
session.setAttribute("username",uname);
pass= request.getParameter("pass");
session.setAttribute("password",pass);
age = request.getParameter("age");
session.setAttribute("age",age);
email = request.getParameter("email");
session.setAttribute("email",email);
dob = request.getParameter("dob");
session.setAttribute("dob",dob);
response.sendRedirect("academic.jsp");
%>
</body>
</html>
Second.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<br>
<h2>Enter Your Academic Details</h2>
<form action="academic.jsp" method="get">
<table><tr><td>
MID: </td><td> <input type="text" name="mid" /><br /><br /></td></tr>
<tr><td>Marks: </td><td> <input type="text" name="marks" /><br /><br /></td></tr>
<tr><td>Salary: </td><td><input type="text" name="salary" /><br /><br /></td></tr>
<tr><td>Stream:</td><td><select name="stream"><option>Java</option><option>dotNET</option><option>Testing</option></select><br /><br /></td></tr>
<tr><td><input type="submit" /></td></tr>
</table>
</form>
<%
out.println(session.getAttribute("name"));
%>
</body>
</html>
You need to do response.sendRedirect() to make the effect in url.
request#forward
Silently passes the control to your another resource,And happens on server side,browser doesn't know about it.
Forward():
For a RequestDispatcher obtained via getRequestDispatcher(), the ServletRequest object has its path elements and parameters adjusted to match the path of the target resource.
sendRedirect()
Sends a temporary redirect response to the client using the specified redirect location URL and clears the buffer.
Highlighting Luggis comment,that move your business logic to Controller and try to avoid scriplets too if possible.
Though,it is not recommended,If you want to change the URL and still want to access the data in second page,one possibility is that put data in session and access in second jsp.
The problem is generated because you have a direct call of forward method in a scriptlet, this might look like this
request.getRequestDispatcher("second.jsp").forward(request, response);
By your question edit, this is generating the problem:
response.sendRedirect("academic.jsp");
Note that using scriptlets is highly discouraged.
Make sure all your data processing and navigation is handled in a Servlet or another controller classes provided by a MVC framework like JSF managed beans or Spring MVC #Controller decorated classes.
More info:
How to avoid Java code in JSP files?
StackOverflow's Servlet wiki, here you can find a real world basic example about how to handle data processing and manipulation from a view to a servlet and then navigating to another view.
The actual problem lies in first.jsp line response.sendRedirect("academic.jsp"); which is inside a JSP Declaration and not JSP Scriptlet, as per the doc variables and methods in JSP declarations become declarations in the JSP page’s servlet class which explains why when you hit the first.jsp its getting redirected to another page without any action and as other suggested its not advisable to use JSP scriptlets, or declarations in your JSP.

Any alternatives to JSPs for open-id authentication in java Spring web applications?

I am interested in making some sort of HTML page that displays the name of a user that was authetnicated through some openid authentication mechanism. I noticed the example in many places I see uses something like:
"j_spring_openid_security_check"
Ive read that JSPs are not recommended for web applications as JSFs are much more popular and I would like to if possible stick with standard HTML. Is there some way to be able to authenticate some other means by maybe calling some sort of service through some GET or POST to get the identity of the user so I wouldnt have to rely on JSPs for my web application?
Any examples/guides/approaches would be great.
You can use a multitude of technologies as views in Spring MVC, as mentioned in the documentation. If you're really set against JSPs, you could use Velocity or Freemarker templates, but there are plenty of successful enterprise applications that use JSPs as their view technologies: the Amazon Web Services forums being but one example.
This is example from my project of clean html text with username/password and openid login forms.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body style="width: 100%; height: 100%">
<div style="width: 100%; vertical-align: middle; height: 100%; border: 1px solid red" align="center">
<form style="border: 1px solid red" action="../j_spring_security_check" method="post">
<p>
<label for="j_username">Username</label> <input id="j_username" name="j_username" type="text" />
</p>
<p>
<label for="j_password">Password</label> <input id="j_password" name="j_password" type="password" />
</p>
<input type="submit" value="Login" />
</form>
<form action="../j_spring_openid_security_check" method="post">
For Google users: <input name="openid_identifier" type="hidden" value="https://www.google.com/accounts/o8/id" /> <input type="submit" value="Sign with Google" />
</form>
</div>
</body>
</html>
You have to change code
<action="../j_spring_security_check">

How do I pre-fill text fields in a form with info from a database in a .jsp?

I'm trying to create a form that updates an entry in a MySQL database. The table is a users table that contains various fields related to the user. I need this page to work as an update form that takes a username that is passed to it via the previous page and pre-fills the text fields with the existing data. It's a model one application that uses a presentation, transport, and data layer. The transport layer is User.java and the data layer (that interacts with the database) is UserDB.java.
Here's the code, everything is functional except the pre-filling.
UPDATE: I've edited to reflect the changes from the answers below, I also want to note that "UserDB.getUsers()" returns an ArrayList.
<%# page language="java" contentType="text/html; charset=iso-8859-1"
pageEncoding="ISO-8859-1" import="java.util.ArrayList,beans.*,data.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
//get parameters from the request
String userName = request.getParameter("userName");
ArrayList userList = UserDB.getUsers(userName);
User user = userList.get(0);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="styles/style.css" type="text/css" />
<title>User Admin</title>
</head>
<body>
<div id="main">
<h1>Update User</h1>
<h3>User Info</h3>
<hr>
<div id="content">
<form action="updateUser.jsp" method="get">
<p>User Name<br>
<input type="text" name="userName" size="20" value="<%=userName%>"/>
</p>
<p>Password<br>
<input type="text" name="password" size="20" value="<%=user.getPassword()%>"/>
</p>
<p>First Name<br>
<input type="text" name="firstName" size="20" value="<%=user.getFirstName()%>"/>
</p>
<p>Last Name<br>
<input type="text" name="lastName" size="20" value="<%=user.getLastName()%>"/>
</p>
<p>Email Address<br>
<input type="text" name="email" size="20" value="<%=user.getEmail()%>"/>
</p>
<p>
<input type="submit" value="Commit Update">
</p>
</form>
</div>
</div>
</body>
</html>
I know I'm doing something wrong with the "user" object, but I'm overlooking it.
you are passing the username as a string
<%
//get parameters from the request
String userName = request.getParameter("userName");
user = UserDB.getUsers("userName");
//it should be (with out the quotes
user = UserDB.getUsers(userName);
%>
The "username" should have the quotes removed. Also, where are you declaring user's type?
ie.
User user = ....getUser..

Categories