servlet + jsp常用代码片断

<!--xml中定义servlet-->
	<servlet>
		<servlet-name>login</servlet-name>
		<servlet-class>xxx.LoginServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>login</servlet-name>
		<url-pattern>/login.do</url-pattern>
	</servlet-mapping>

<!--JSP中指定编码-->
<%@ page contentType="text/html; charset=UTF-8" %>
//servlet里放东西到request中
request.setAttribute("userName", user.getName());

//从servlet转到jsp
			RequestDispatcher view = request.getRequestDispatcher("/view.jsp");
			view.forward(request, response);

<!--JSP中显示request中的变量-->
<div> Hello,  <%=request.getAttribute("userName")%> </div>

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.