Web Science/Part1: Foundations of the web/Dynamic Web Content/Handling a post request in a Java Servlet
< Web Science‎ | Part1: Foundations of the web‎ | Dynamic Web Content
edit MOOC index
Handling a post request in a Java Servlet
Learning goals
Video
Script
Quiz
Further reading
Discussion



Learning goals
  1. See how a POST request is handled in a Java Servlet
  2. get to know the Request object
  3. see how a data base query and more advanced technology can be included to a servlet



Video



Script
Register.java
import java.io.IOException​;​import javax.servlet.ServletException​;​import javax.servlet.http.HttpServlet​;​import javax.servlet.http.HttpServletRequest​;​import javax.servlet.http.HttpServletResponse​;​import org.eclipse.jetty.server.Server​;​import org.eclipse.jetty.webapp.WebAppContext​;​public class Register extends HttpServlet { private Connection connect = null; private Statement statement = null; private ResultSet resultSet = null; public Register() { try { Class​.​forName​(​"com.mysql.jdbc.Driver"​); // Setup the connection with the DB connect = DriverManager .​getConnection​(​"jdbc:mysql://localhost/mooc?" + "user=mooc&password=studywebscience"​); // Statements allow to issue SQL queries to the database statement = connect​.​createStatement​(); // Result set get the result of the SQL query } catch (​ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doGet​(​HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp​.​getWriter​().​write​(​"hello world"); } protected void doPost​(​HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String sql = "insert into mooc.users values(default, '" + req​.​getParameter​(​"username"​) + "', '" + req​.​getParameter​(​"email"​) + "')"; resp​.​setContentType​(​"text/html"​); try { statement​.​execute​(​sql​); resp.getWriter().write( "received username:<b>" + req​.​getParameter​(​"username"​) + "</b>, with email: <em>" + req​.​getParameter​(​"email"​) + "</em>"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) throws Exception { Server server = new Server(8080); WebAppContext context = new WebAppContext(); context​.​setDescriptor​(​"WEB-INF/web.xml"​); context​.​setResourceBase​(​""​); context​.​setContextPath​(​""​); context​.​setParentLoaderPriority​(​true​); server​.​setHandler​(​context​); server.start(); server.join(); }}



Quiz
1How can HTTP header fields of an HTTP request be accessed in the Java code snippet which uses Jetty as an embedded servlet?
Retrieve the data from the socket using a reader class.
Retrieve the data from the socket using an input stream class.
There are methods on the request object for accesing HTTP header fields
Inside of the doGet and doPost methods exist objects which have access to the header fields.
2What should one do when accessing request parameters or HTTP headers and processing them inside a server?
The user input retrieved from the fields should be escaped before running database queries
The video demonstrated everything that should happen. Nothing more needs to be done.



Further reading
no further reading defined
You can define further reading here.
In general you can use the edit button in the upper right corner of a section to edit its content.

Discussion

Navigation
  1. Ethernet
  2. Internet Protocol
  3. Transmission Control Protocol
  4. Domain Name System
  5. Internet vs World Wide Web
  6. Hypertext Transfer Protocol
  7. Web content
  8. Dynamic Web Content
    1. Basics of server side web programming
    2. Forms and HTTP post request
    3. Handling a post request in a Java Servlet
    4. Client side JavaScript
    5. Ajax and the XMLHttpRequest class
    6. Summary, further reading, homework

Forms and HTTP post request

Client side JavaScript
Last edited on 17 November 2014, at 15:57
Wikiversity
Content is available under CC BY-SA 3.0 unless otherwise noted.
Privacy policy
Terms of Use
Desktop
HomeRandomLog inSettingsDonateAbout WikiversityDisclaimers
WatchEdit