@import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
参照一下地址,搭建nginx tomcat的负载均衡环境
新建一个简单的web应用,实现简单的登陆过程
User java bean
1 package com.xxx.yyy.entity;
2
3 public class User {
4
5 private String username;
6 private String password;
7
8 public String getUsername() {
9 return username;
10 }
11 public void setUsername(String username) {
12 this.username = username;
13 }
14 public String getPassword() {
15 return password;
16 }
17 public void setPassword(String password) {
18 this.password = password;
19 }
20
21 }
22
登录Servlet
1 package com.xxx.yyy.servlet;
2
3 import java.io.IOException;
4 import javax.servlet.annotation.WebServlet;
5 import javax.servlet.ServletException;
6 import javax.servlet.http.HttpServlet;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9 import javax.servlet.http.HttpSession;
10
11 import com.xxx.yyy.entity.User;
12 @WebServlet(name = "LoginServlet", urlPatterns = "/login.do")
13 public class LoginServlet extends HttpServlet {
14
15 @Override
16 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
17 throws ServletException, IOException {
18
19
20 String username = req.getParameter("username");
21 String password = req.getParameter("password");
22
23 User user = new User();
24 user.setUsername(username.trim());
25 user.setPassword(password.trim());
26
27 if("root".equals(user.getUsername())&& "admin".equals(user.getPassword())) {
28 HttpSession session = req.getSession();
29 session.setAttribute("user", user);
30 resp.sendRedirect("success.jsp");
31 } else {
32 resp.sendRedirect("failure.jsp");
33 }
34 }
35
36 }
37
登录页面index.jsp
1 <%@ page language="java" contentType="text/html; charset=windows-31j"
2 pageEncoding="windows-31j"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
7 <title>Login Page</title>
8 </head>
9 <body>
10 <form action="login.do" method="post">
11 User Name:<input type="text" name="username"><br>
12 Password:<input type="password" name="password"><br>
13 <input type="submit">
14 </form>
15 </body>
16 </html>
17
登录成功后,显示的页面 success.jsp
1 <%@ page language="java" contentType="text/html; charset=windows-31j"
2 pageEncoding="windows-31j"%>
3
4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5 <html>
6 <head>
7 <meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
8 <title>Login Success Page</title>
9 </head>
10 <body>
11 Login Success<br>
12
13
14
15 Username:${sessionScope["user"].username}
16 <br>
17 Password:${sessionScope["user"].username}
18
19 </body>
20 </html>
21
登录失败后,显示的页面 failure.jsp
1 <%@ page language="java" contentType="text/html; charset=windows-31j"
2 pageEncoding="windows-31j"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
7 <title>Login Failure Page</title>
8 </head>
9 <body>
10 Login Failure
11 </body>
12 </html>
13
Web应用新建完了之后,需要修改nginx.conf文件,修正内容如下:
location ~ \.(jsp|do)$
{
proxy_pass http://local_tomcats;
#root html;
#index index.html index.htm;
}
由于我们servlet的url是以.do结尾的,因此所有以.do结尾的请求都转给Tomcat处理。
修正之后运行nginx -s reload,重新加载配置文件
实际运行效果如下:
点击“提交”按钮,显示如下页面
提示在success.jsp的15行抛空指针异常。即如下代码:
Username:<%=
user.getUsername()%>
从session对象中取出的user为null,为什么会出现这种情况。因为session对象是保存在tomcat服务器上的,session在两台tomcat之间没有共享,导致了生成包含user对象的session在一台服务器上,而获取的session并不是包含user对象的session。访问两台Tomcat的权重是一样的
server localhost:8080 weight=1;
server localhost:18080 weight=1;
为了解决上面的问题,只是简单修正一下nginx.conf文件,修正内容如下:
#server
localhost:8080 weight=1;
#server localhost:18080 weight=1;
server
localhost:8080;
server
localhost:18080;
ip_hash;
修正之后运行nginx -s reload,重新加载配置文件
实际运行效果如下:
# nginx中的ip_hash技术能够将某个ip的请求定向到同一台后端,这样一来这个ip下的某个客户端和某个后端就能建立起稳固的session
当然解决session共享问题,还可以通过memcache来解决,后续文章会详细介绍。