public class ControllerServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
//path="XXXX.do"
String path = request.getServletPath();
//去掉.do
path = path.substring(0, path.length() - 3);
if(path.equals("/add"))
{
//Struts中会由org.apache.struts.action.ActionServlet调用Action
//do something
}else if(path.equals("/modify"))
{
//do something
}else if(path.equals("/delete"))
{
//do something
}else if(path.equals("/find"))
{
//do something
}else if(path.equals("/tomodify"))
{
//do something
}else if(path.equals("/login"))
{
//do something
}else
{
throw new ServletException("unknown path:" + path);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
doGet(request, response);
}
}
=====================================================
举例:
package com.allanlxf.ums.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.allanlxf.ums.entity.User;
import com.allanlxf.ums.service.UserService;
public class ControllerServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
// UserService service = null;
// synchronized(getServletContext())
// {
// service = (UserService)getServletContext().getAttribute("service");
// if(service == null)
// {
// service = new UserService();
// getServletContext().setAttribute("service", service);
// }
// }
UserService service = (UserService)getServletContext().getAttribute("service");
String path = request.getServletPath();
path = path.substring(0, path.length() - 3);
if(path.equals("/add"))
{
String name = request.getParameter("name");
User user = new User();
user.setName(name);
try
{
service.add(user);
forward("/find.do", request, response);
}catch(Exception e)
{
e.printStackTrace();
forward("/error.html", request, response);
}
}else if(path.equals("/modify"))
{
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
User user = new User();
user.setId(id);
user.setName(name);
try
{
service.modify(user);
forward("/find.do", request, response);
}catch(Exception e)
{
e.printStackTrace();
forward("/error.html", request, response);
}
}else if(path.equals("/delete"))
{
int id = Integer.parseInt(request.getParameter("id"));
try
{
service.delete(id);
forward("/find.do", request, response);
}catch(Exception e)
{
e.printStackTrace();
forward("/error.html", request, response);
}
}else if(path.equals("/find"))
{
Collection users = service.findAll();
request.setAttribute("users", users);
forward("/user/list", request, response);
}else if(path.equals("/tomodify"))
{
int id = Integer.parseInt(request.getParameter("id"));
User user = service.findById(id);
request.setAttribute("user", user);
forward("/user/modify", request, response);
}else if(path.equals("/login"))
{
String userName = request.getParameter("userName");
String password = request.getParameter("password");
try
{
User user = service.login(userName, password);
if(user != null)
{
HttpSession session = request.getSession(true);
session.setAttribute("user", user);
forward("/find.do", request, response);
}else
{
forward("/login.html", request, response);
}
}catch(Exception e)
{
e.printStackTrace();
forward("/error.html", request, response);
}
}else
{
throw new ServletException("unknown path:" + path);
}
System.out.println(path);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
doGet(request, response);
}
private void forward(String path, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(path);
dispatcher.forward(request, response);
}
}
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
//path="XXXX.do"
String path = request.getServletPath();
//去掉.do
path = path.substring(0, path.length() - 3);
if(path.equals("/add"))
{
//Struts中会由org.apache.struts.action.ActionServlet调用Action
//do something
}else if(path.equals("/modify"))
{
//do something
}else if(path.equals("/delete"))
{
//do something
}else if(path.equals("/find"))
{
//do something
}else if(path.equals("/tomodify"))
{
//do something
}else if(path.equals("/login"))
{
//do something
}else
{
throw new ServletException("unknown path:" + path);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
doGet(request, response);
}
}
=====================================================
举例:
package com.allanlxf.ums.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.allanlxf.ums.entity.User;
import com.allanlxf.ums.service.UserService;
public class ControllerServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
// UserService service = null;
// synchronized(getServletContext())
// {
// service = (UserService)getServletContext().getAttribute("service");
// if(service == null)
// {
// service = new UserService();
// getServletContext().setAttribute("service", service);
// }
// }
UserService service = (UserService)getServletContext().getAttribute("service");
String path = request.getServletPath();
path = path.substring(0, path.length() - 3);
if(path.equals("/add"))
{
String name = request.getParameter("name");
User user = new User();
user.setName(name);
try
{
service.add(user);
forward("/find.do", request, response);
}catch(Exception e)
{
e.printStackTrace();
forward("/error.html", request, response);
}
}else if(path.equals("/modify"))
{
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
User user = new User();
user.setId(id);
user.setName(name);
try
{
service.modify(user);
forward("/find.do", request, response);
}catch(Exception e)
{
e.printStackTrace();
forward("/error.html", request, response);
}
}else if(path.equals("/delete"))
{
int id = Integer.parseInt(request.getParameter("id"));
try
{
service.delete(id);
forward("/find.do", request, response);
}catch(Exception e)
{
e.printStackTrace();
forward("/error.html", request, response);
}
}else if(path.equals("/find"))
{
Collection users = service.findAll();
request.setAttribute("users", users);
forward("/user/list", request, response);
}else if(path.equals("/tomodify"))
{
int id = Integer.parseInt(request.getParameter("id"));
User user = service.findById(id);
request.setAttribute("user", user);
forward("/user/modify", request, response);
}else if(path.equals("/login"))
{
String userName = request.getParameter("userName");
String password = request.getParameter("password");
try
{
User user = service.login(userName, password);
if(user != null)
{
HttpSession session = request.getSession(true);
session.setAttribute("user", user);
forward("/find.do", request, response);
}else
{
forward("/login.html", request, response);
}
}catch(Exception e)
{
e.printStackTrace();
forward("/error.html", request, response);
}
}else
{
throw new ServletException("unknown path:" + path);
}
System.out.println(path);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
doGet(request, response);
}
private void forward(String path, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(path);
dispatcher.forward(request, response);
}
}