Reputation: 149
I want to use one servlet for four action CRUD. following servlet works well for adding data by following url
response in json like {"result":"true","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();
}
}
}
I want use same servlet for other action like delete , view etc by url(it should use only one above servlet for actions)
http://localhost:8080/mobsurvey/view-company/?company_id=1
should response it data in json
currently 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>
I am newbie in java-servlet
Any Suggestions ? Thank You.
Upvotes: 1
Views: 3224
Reputation: 4842
Not an answer to your question really. There are several ways to do what you want to achieve, one of them is what @hope_is_grim has put forth. Using a Single Servlet for a number of CRUD operations is not a scalable solution, IMHO. I've been there, done that and later switched to pure RESTful web services using libraries like Jersey or RESTEasy. It makes the code maintainable, scalable and readable. The sooner you opt, the better.
Useful thread
Upvotes: 1
Reputation: 1934
You can add some extra information in your URL.
Like parameters
?action=edit&id=1
And in your servlet you can use the action parameter for decision making
String action = request.getParameter("action");
if(action.equal(...) { /* Your logic here */ }
Or you can implement your own dispatcher to route request with URL like
company/edit/1
Or you can override the service() method to support PUT, DELETE, ... and make your servlet RESTful. But browsers' support for those methods is not very good.
Upvotes: 3