I have a simple servlet at the moment. I need it to call a static jar method and return its response.
1) Can someone recommend a suitable structure ie do I need to create factories and handler classes etc…
2) How should I encode the hash in the response … Is the simply writing to output stream ok do something else ? Ie write to headers etc.. Another App will be reading the response.
3) Not sure about handling nulls etc..
package myproj.servlet;
//Std imports
//Serlet imports
import myproj.hash.HashGenerator;
public class MyServlet extends HttpServlet{
String hashcode;
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String accountName = req.getParameter("accountName");
try {
hashcode = HashGenerator.hash(accountName);
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
//[EDIT] was returning accountName by accident
out.print(hashcode );
} catch (Exception e) {
//Some logging code
}
}
First and foremost, your
hashcodeis not thread-safe.After looking to your provided snippet. I don’t think so. It is fairly simple thing.
You are getting
accountNameas parameter, and then writing the same back. Is that you want? Or you mean respond the hashed version.You might like to validate
accountNameparameter, since it is the input provided by user.