java如何把值传到类,如何从一个Java类传递一个变量的值到JSP页面

I have 2 files named Admin.java and index.jsp.

In Admin.java through a function I retrieve the value of the varible named res. This variable needs to be passed to a JSP page.

The Admin.java is in C:\Users\praveen\workspace\SemanticWeb\src\controller whereas the index.jsp is in C:\Users\praveen\workspace\SemanticWeb\WebContent.

The code of Admin.java is:

public Admin()

{

super();

}

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException

{

if (action.equals("login"))

{

String userName="";

String password="";

userName = request.getParameter("username");

password = request.getParameter("password");

response.setCharacterEncoding("UTF-8");

SemanticSearch semsearch = new SemanticSearch(request.getSession());

semsearch.loadData(REALPATH + RDFDATASOURCEFILE1);

String res=semsearch.searchForUser(userName, password);

System.out.println("The value of res been passed is "+res);

request.setAttribute("rest", res);

return;

}

The code of index.jsp is

function login(user, pass)

{

$.ajax({

type:"GET",

url: "Admin?action=login",

dataType: "text",

data: { username: user, password: pass },

success: function(response){

}

within the

function(response)

{

......

}

I need to access the value of res passed by Admin.java. I am not able to get any proper help for my code on the Internet. Please can someone help me with this.

解决方案

From your code,

request.setAttribute("rest", res);

You shouldn't set it as request attribute. Setting request attributes is only useful if you're forwarding to a JSP file. You need to write it straight to the response yourself. Replace the line by

response.getWriter().write(res);

This way it'll end up in the response body and be available as variable response in your JS function.

See also:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值