JSP & Servlets
1.0.0
1.0.0
  • Setting up
  • Pre-requisites
  • Servlet Part 1
  • Servlet Part 2
  • Understanding the servlet
  • Servlet XML Configuration
  • POST method and passing parameters
  • Passing more parameters
  • Understanding GET and POST
  • Request, Session and Context Part -1
  • Request , Session and Context Part - 2
  • Understanding init, service and ServletConfig
  • Hello JSP
  • Understanding JSP
  • JSP Page directives
  • HttpServletRequest Path Decoding
  • Scopes in JSP and the PageContext Object
  • Understanding MVC pattern
  • PART -1 Writing an MVC app
  • JSTL
    • For-each
  • CRUD-1
  • Project Work
    • Create a Simple Java Web Application Using Servlet, JSP/JSTL and JDBC/Transaction
  • Locale Filter
  • Action Plan
    • Schedule
    • Hit webservice
Powered by GitBook
On this page
  • Step 1: Passing data using parameters in GET method:
  • Step 2: Hit the URL and add the parameter as "?userName=Mohit"
  • Step 3: In order to test POST create a simple html form:
  • Step 4: Create doPost method in servlet:

Was this helpful?

POST method and passing parameters

Step 1: Passing data using parameters in GET method:

package com.gs.controller;

import java.io.IOException;
import java.io.PrintWriter;

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

public class XmlServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("Hello from Get method of XML servlet");
		response.setContentType("text/html");
		String userName =  request.getParameter("userName");
		PrintWriter printWriter = response.getWriter();
		printWriter.println("Hello from GET method "+userName);

	}

}

Step 2: Hit the URL and add the parameter as "?userName=Mohit"

Step 3: In order to test POST create a simple html form:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	Hello World
	<form method="post" action="xmlservletpath">
		<input name="userName" /> 
		<input type="submit" />
	</form>
</body>
</html>

Step 4: Create doPost method in servlet:

package com.gs.controller;

import java.io.IOException;
import java.io.PrintWriter;

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

public class XmlServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("Hello from Get method of XML servlet");
		response.setContentType("text/html");
		String userName = request.getParameter("userName");
		PrintWriter printWriter = response.getWriter();
		printWriter.println("Hello from GET method " + userName);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("Hello from POST method of XML servlet");
		response.setContentType("text/html");
		String userName = request.getParameter("userName");
		PrintWriter printWriter = response.getWriter();
		printWriter.println("Hello from POST method " + userName);

	}

}
PreviousServlet XML ConfigurationNextPassing more parameters

Last updated 6 years ago

Was this helpful?

http://localhost:8080/SimpleServletProject/xmlservletpath?userName=Mohitlocalhost