IntelliJ IDEA 2017 서브렛 구성

4535 단어 IntelliJIDEA
예전에 My Eclipse에 서브렛을 썼으면 웹에서xml 쓰기 일치
그러나 IntelliJ IDEA 에서는 조금 다르고, 더욱 편리한 방법을 채택하였다
아까 쓴 작은 데모를 예로 들자.
JSP 페이지 코드:



    Title


:

:


서브렛 코드
package com.limbo.Servlet;

import com.limbo.Bean.Contacts;
import com.limbo.Bean.Group;
import com.limbo.Bean.Page;
import com.limbo.Bean.Users;
import com.limbo.Dao.ContactDao;
import com.limbo.Dao.ImplDao.ContactBookImplDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "Servlet",urlPatterns = "/Servlet") //            ,urlPatterns Servlet   
public class Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        String method = request.getParameter("method");
        if (method!=null&&!"".equals(method)){
            if("Show".equals(method))
            {
                Show(request,response);
            }else if("Login".equals(method))
            {
               Login(request,response);
            }
            else if ("Add".equals(method)) {
               Add(request, response);
            }
        }
    }
    protected void Show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ContactDao contactDao=new ContactBookImplDao();
        Contacts contacts=new Contacts();

        String currpage=request.getParameter("currPage");
        if (currpage == null) {
            currpage = "0";
        }
        Page page=contactDao.showAllContacts(contacts,Integer.parseInt(currpage));
        HttpSession session = request.getSession();
        session.setAttribute("contacts_list",page);
        response.sendRedirect("Show.jsp");

    }
    protected void Login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ContactDao contactDao=new ContactBookImplDao();
        Users users=contactDao.login(request.getParameter("Loginname"),request.getParameter("password"));
        HttpSession session=request.getSession();
        session.setAttribute("users_order",users.getUsers_order());
        if (users!=null){
            response.sendRedirect("/Servlet?method=Show");
        }else {
            response.sendRedirect("error.jsp");
        }

    }
    protected void Add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
        Group group=new Group();
        Contacts contacts=new Contacts();
        ContactDao contactDao=new ContactBookImplDao();
        HttpSession session=request.getSession();


        contacts.setContacts_group(request.getParameter("GroupId")); //  
        contacts.setContacts_name(request.getParameter("Contacts_Name"));//  
        contacts.setContacts_sex(request.getParameter("Contacts_Sex"));//  
        contacts.setContacts_tel(Integer.parseInt(request.getParameter("Contacts_Tel")));//  
        contacts.setUsers_order((Integer) session.getAttribute("users_order")); //  ID
        String getBirth=request.getParameter("Contacts_Birth");//  

        Date Contacts_Birth= null;
        try {
            Contacts_Birth = fmt.parse(getBirth);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        contacts.setContacts_birth(Contacts_Birth);

        contactDao.addContacts(contacts);
        response.sendRedirect("/Servlet?method=Show");

    }

}

좋은 웹페이지 즐겨찾기