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

View File

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