一个servlet java中的多个动作(CRUD)

时间:2012-03-01 05:35:56

标签: java json servlets url-routing crud

我想将一个servlet用于四个动作CRUD。以下servlet适用于通过以下URL添加数据

http://localhost:8080/mobsurvey/register-company/?companyname=xyz&email=abc@abc.com&username=user&password=pass&description=xyz

在json中的回应就像 { “结果”: “真”, “lastId”:2}

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.*; 
import com.saffroze.Event;




public class CompanyRegister extends HttpServlet {
Connection conn=null;
public CompanyRegister() throws InstantiationException, IllegalAccessException {

    conn = Dbhelper.getConnection();
    //      creatBookManagerTable();
}

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {


    String companyname = req.getParameter("companyname");
    String email = req.getParameter("email");
    String usr = req.getParameter("username");
    String pwd = req.getParameter("password");
    String description = req.getParameter("description");



    PreparedStatement pst=null;
    String sql = "insert into company("+ "name,"+ "email,"+ "username,"+ "password,"+ "description)"+" values(?,?,?,?,?)";      

    try {

        PreparedStatement pstmt = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
        //String insertDataSql="insert into company(name,email,username,password,description) values('" + companyname + "','" + email + "','" + usr + "','" + pwd + "','" + description + "');";

        //Statement stmt;

        pstmt.setString(1,companyname);
        pstmt.setString(2,email);
        pstmt.setString(3,usr);
        pstmt.setString(4,pwd);
        pstmt.setString(5,description);

        Gson gson = new Gson();
        //resp.setContentType("application/json; charset=UTF-8");
        resp.setContentType("text/plain");
        pstmt.executeUpdate();
        ResultSet keys = pstmt.getGeneratedKeys();  

        keys.next();  
        int id = 0; 

        id = keys.getInt(1);  

        keys.close();  



        if(id>0){

            Event obj = new Event("true",id);

            resp.getWriter().println(gson.toJson(obj));     
        }else
        {
            int emptyid=0;
            Event obj = new Event("false",emptyid);


            resp.getWriter().println(gson.toJson(obj));


        }

    }

    catch(Exception e){

        e.printStackTrace();
    }

}

}

我想使用相同的servlet进行其他操作,例如url的删除,查看等(它应该只使用一个以上的servlet进行操作)

http://localhost:8080/mobsurvey/view-company/?company_id=1

应该在json中响应它的数据

目前是web.xml

  <display-name>mobsurvey</display-name>
  <servlet>
 <servlet-name>CompanyRegister Servlet</servlet-name>
 <servlet-class>com.saffroze.CompanyRegister</servlet-class>

 </servlet>
 <servlet-mapping>
 <servlet-name>CompanyRegister Servlet</servlet-name>
 <url-pattern>/register-company/*</url-pattern>
 </servlet-mapping>

 <welcome-file-list>
 <welcome-file>index.html</welcome-file>
  </welcome-file-list>
 </web-app> 

我是java-servlet的新手

有什么建议吗? 谢谢。

2 个答案:

答案 0 :(得分:2)

您可以在网址中添加一些额外信息 像参数一样

?action=edit&id=1

在您的servlet中,您可以使用action参数进行决策

String action = request.getParameter("action");
if(action.equal(...) { /* Your logic here */ }

或者您可以实现自己的调度程序来路由请求,例如

company/edit/1

或者您可以覆盖service()方法以支持PUT,DELETE,...并使您的servlet RESTful。但浏览器支持这些方法is not very good

答案 1 :(得分:1)

真的不是你问题的答案。有几种方法可以实现您想要实现的目标,其中之一就是@hope_is_grim提出的方法。对于许多CRUD操作使用单个Servlet不是可扩展的解决方案,恕我直言。我去过那里,完成了这项工作,后来又使用JerseyRESTEasy等库转换为纯RESTful Web服务。它使代码可维护,可扩展和可读。你越早选择,就越好。

有用的帖子