您如何将Java与html和javascript混合使用? [英] How do you mix up java with html and javascript?

查看:180
本文介绍了您如何将Java与html和javascript混合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的Java项目,必须在其中使用jsp文件.
我有一个带有登录按钮的html,可触发以下功能:

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);
});
}

据我所知,发出了一个httpRequest并将其与数据一起发送到baseUrl + loginCall,这意味着localhost/something/user/login?name = somename& pass = somepass

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

这是我遇到的问题,我是否必须以某种方式在某个地方制作一个Java文件,以接收请求信息,对数据库进行处理并返回答案?
如果是这样,在哪里,如何?我必须将其命名为login/user.java吗?

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?

推荐答案

您需要再次了解JSP MVC jsp页面应包含html,javascript和java代码.如果要调用一个单独的.java类,则需要将该类编写为servlet,然后对其进行调用.

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.

因此,在您的.jsp文件中,您就拥有html和javascript了,就像在那儿一样,然后在这些括号中包含的所有Java<%%>

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

在此处查看教程 http://www.jsptut.com/

我看到您正在执行登录页面.我使用了这个出色的教程来创建一个登录系统,该系统帮助我了解了jsp和servlet的工作方式. http://met.guc.edu.例如/OnlineTutorials/JSP%20-%20Servlets/Full%20Login%20Example.aspx

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

也请查看此图片,这应该有助于您理解概念.请记住,servlet是纯Java类,主要用于java,但也可以输出html,jsp用于大多数html(& javascript),但可以包含jsp.因此,servlet完成工作,然后jsp获取计算值,以便JavaScript可以利用它们.无论如何,我就是这样想的,可能是错误的 http://met.guc.edu.eg/OnlineTutorials/static/article_media/jsp%20-%20servlets/LoginExample%20 [4] .jpg

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

祝一切顺利

这篇关于您如何将Java与html和javascript混合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆