Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet how to point my jsp form action to servlet?

Have Programming Question? Ask it here!
 
 
 


anson
how to point my jsp form action to servlet?
1 Answer(s)      4 years and 3 months 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();
}

Advertisement
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


Related Tutorials/Questions & Answers:
Advertisements
Advertisements
 

 

 

DMCA.com