changes for servlet

This commit is contained in:
Karan Khanna 2018-05-20 13:00:25 +02:00
parent 8123ba6442
commit e375a00a09
2 changed files with 20 additions and 20 deletions

View File

@ -18,6 +18,11 @@
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet.version}</version> <version>${javax.servlet.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
@ -30,11 +35,6 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>

View File

@ -14,24 +14,24 @@ import com.google.gson.Gson;
@WebServlet(name = "EmployeeServlet", urlPatterns = "/employeeServlet") @WebServlet(name = "EmployeeServlet", urlPatterns = "/employeeServlet")
public class EmployeeServlet extends HttpServlet { public class EmployeeServlet extends HttpServlet {
@Override @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException { throws IOException {
int id = Integer.parseInt(request.getParameter("id")); int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name"); String name = request.getParameter("name");
String department = request.getParameter("department"); String department = request.getParameter("department");
Double salary = Double.parseDouble(request.getParameter("salary")); Double salary = Double.parseDouble(request.getParameter("salary"));
Employee employee = new Employee(id, name, department, salary); Employee employee = new Employee(id, name, department, salary);
String employeeJsonString = new Gson().toJson(employee);
PrintWriter out = response.getWriter();
String employeeJsonString = new Gson().toJson(employee); PrintWriter out = response.getWriter();
response.setContentType("application/json"); response.setContentType("application/json");
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
out.print(employeeJsonString); out.print(employeeJsonString);
out.flush(); out.flush();
} }
} }