Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

how to point my jsp form action to servlet?

Ads

 
 

Share on Google+Share on Google+

anson
how to point my jsp form action to servlet?
1 Answer(s)      7 years ago
Posted in : JSP-Servlet
I am currently using netbean 6.8, tomcat 6.0, and mysql database.
I was having problem when I was trying to point my jsp action to my servlet.(f.action="../fyp.servletLogin";) fyp is the folder that stores my servlet.
Here is my function code in jsp, when user click on submit button, it will calls this login function to pass value to servlet.

function login() {
var f = document.index;
var length;
var userID = f.UserID.value;
var userPwd = f.UserPwd.value;

length = userID.length;
if (length < 1) {
alert("User ID is null. Please key in User ID.");
f.UserID.focus();
return(false);
}

length = userPwd.length;
if (length < 1) {
alert("Password is null. Please key in Password.");
f.UserPwd.focus();
return(false);
}
alert(userPwd.valueOf());
alert(userID.valueOf());
f.method = "POST";
f.action = "../fyp.servletLogin";
f.submit();
}
Ads
View Answers

April 15, 2010 at 12:36 PM


Hi Friend,

Try the following code:

1)login.jsp:

<script>
function login() {
var f = document.form;
var length;
var userID = f.UserID.value;
var userPwd = f.UserPwd.value;

length = userID.length;
if (length < 1) {
alert("User ID is null. Please key in User ID.");
f.UserID.focus();
return(false);
}

length = userPwd.length;
if (length < 1) {
alert("Password is null. Please key in Password.");
f.UserPwd.focus();
return(false);
}
//alert(userPwd.valueOf());
//alert(userID.valueOf());
f.method = "POST";
f.action = "../servletLogin?user="+userID+"&&pass="+userPwd;
f.submit();
}
</script>
<form name="form">
<table>
<tr><td>Username</td><td><input type="text" name="UserID"></td></tr>
<tr><td>Password</td><td><input type="text" name="UserPwd"></td></tr>
<tr><td><input type="button" value="Submit" onclick="return login();"></td></tr>
</table>
</form>

2)servletLogin.java

import javax.servlet.http.*;

public class servletLogin extends HttpServlet {

public void doPost (HttpServletRequest request,HttpServletResponse response) {
String username=request.getParameter("user");
String password=request.getParameter("pass");

}
}

Thanks

Ads