java和javascript配合,您如何将Java与html和javascript混合使用?

I have this little java project in which I have to use jsp files.

I have an html with a login button that triggers the following function:

var loginCall = "user/login";

var logoutCall = "user/logout";

var signupCall = "user/signup";

function login() {

var login = baseUrl + loginCall + "?";

var loginFormElements = document.forms.loginForm.elements;

login = addParam(login, USER_NAME, loginFormElements.userName.value, false);

login = addParam(login, PASSWORD, loginFormElements.password.value, true);

simpleHttpRequest(login, function(responseText){

var status = evalJSON(responseText);

if (status.errorCode == 200) {

var res = status.results;

var sessionId = res[0].sessionId;

setCookie(SESSION_ID,sessionId);

window.location="http://localhost:8080/"+baseUrl+"/main.html";

} else {

showError(status.errorCode, "Username or password was incorrect.")

}

}, function(status, statusText){console.log('z');

showError(status, statusText);

});

}

As far as I can see a httpRequest is made and sent with data to baseUrl + loginCall, meaning localhost/something/user/login?name=somename&pass=somepass

This is where I'm stuck, do I have to make a java file somewhere somehow, that takes the request information, works it up with the database and returns an answer?

If so, where, how? Do I have to name it login/user.java?

Can anyone point me to the right direction, if not give me some code example or explanation of what I have to do next?

解决方案

You need to have another look at the JSP MVC

The jsp page should hold the html and javascript and java code. If you want to call a separate .java class, you need to write that class as a servlet then call it.

So in your .jsp you have you html and javascript just like you have it there, then any java you include in these brackets

Have a look at the tutorials here http://www.jsptut.com/

And i see your doing a login page. I used this brilliant tutorial for creating a log in system which helped me understand how jsp and servlets worked.

http://met.guc.edu.eg/OnlineTutorials/JSP%20-%20Servlets/Full%20Login%20Example.aspx

Also check out this image which should help you understand the concept. Remember servlets are pure java classes, used for mostly java but can also output html, jsp's are used for mostly html (& javascript) but can include jsp. So the servlets do the work, then the jsp gets the computed values so that they can be utilized by JavaScript. that's how i think of it anyway, may be wrong

http://met.guc.edu.eg/OnlineTutorials/static/article_media/jsp%20-%20servlets/LoginExample%20[4].jpg

All the best

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值