jsp页⾯中js代码中串插java代码⼀说明
尽管现在的趋势是前后端彻底分离的开发模式,但是熟悉⼀下以前的⼀些内容也未尝不可。
⼆具体实现
事实上这个是⾮常容易实现的,因为在.jsp页⾯⾥⾯写java代码本⾝就很简单,下⾯就是所有例⼦。
1. User类
package cn.smileyan.domain;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2 servlet类
package cn.smileyan.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.smileyan.domain.User;
/**
* Servlet implementation class HelloServlet
*/
el表达式和jquery的区别public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  request.setAttribute("Hello", "Hello I am servlet");
User user = new User();
user.setPassword("<h1>I am password!</h1>");
user.setUsername("<h2>I am username!</h2>");
request.setAttribute("user", user);
RequestDispatcher rs = RequestDispatcher("hello.jsp");
rs.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  doGet(request, response);
}
}
3. jsp代码
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="HelloServlet">点我进⾏测试</a>
</body>
</html>
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="cn.smileyan.domain.User"  %>
<%
User user = (Attribute("user");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="cdn.bootcss/jquery/1.10.2/jquery.min.js"></script>
<title>js中穿插EL表达式例⼦</title>
<script>
$(document).ready(function(){
$("#me").click(function(){
$("#me").html("<%=Username() %>");
});
$("#password").click(function(){
$("#password").html("<%=Password() %> ");
});
});
</script>
</head>
<body>
<h3 id="me">click me and I will be changed to username</h3>
<h3 id="password">click me and I will be changed to password </h3>
</body>
</html>
当然最后记得配置Servlet
<servlet>
<servlet-name>HelloServlet</servlet-name>
<display-name>HelloServlet</display-name>
<description></description>
<servlet-class>cn.smileyan.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
⼤功告成!点击前端界⾯就可以看到前端输出的后台的User类的对象的信息。
需要注意的地⽅:
1. hello.jsp中要添上 import="cn.smileyan.domain.User 也就是在jsp的顶部导⼊这个类
2. 在前⾯添加
<%
User user = (Attribute("user");
%>
来读取后台传过来的数据。
⼤功告成!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。