Scripts: JSP :: form processing :: Library Article #3

Developer's Section

Processing Form Data in JSP
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Average Votes: (2529)
Rate It:   

Learn simple techniques to process form data (HTTP post/get) requests using JSP.

This tutorial explains the handling of http POST/ GET data using JSP. The java server side programming language (JSP) provides the request.getParameter method to get POST data after a request has been made to the server.

Let's start by creating a simple form in a file named process_login.jsp:

 Code Snippet 1

<form name="login" action="process_login.jsp" method="post">
  <table align="center" border="0" cellspacing="0" cellpadding="0">
          <td colspan="2" class="center">
          if (showStr != null) {
              out.println("<span class=\"red\">" + showStr + "</span>");
          } else {
              out.println(" ");
      <tr><td colspan="2"></td></tr>
          <td><input type="text" name="username" size="18" 
          value="" maxlength="20" /></td>
          <td><input type="password" name="password" size="18" 
           value="" maxlength="20" /></td> 
      <tr><td colspan="2" class="center">
          <input class="button" type="reset" value="Clear" />
          <input class="button" type="submit" name="login" value="Login" />

So, in order to process the two fields from this form, we are going to add some JSP code to process_login.jsp which will be script handling form data:

 Code Snippet 2

<%@ page language="java" session="true" contentType="text/html" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.DateFormat" %>


//process form http post request

login = request.getParameter("login"); 

String showStr = ""; //use in case error exists

if ((login != null) && (login.equals("Login"))) 
    username = request.getParameter("username");
    password = request.getParameter("password");

    if(username.equals("myusername") && password.equals("mypassword"))
       //set session variable
       session.setAttribute("usrSet", username);

       String newLoc = "/newpath/login_success.html";

        showStr = "There was an error logging you in";
        showStr = showStr + "<br>Please Try again!";


See other Scripts in form processing


Submit Your Scripts:

If you would like to have your JSP scripts published in this section please fill out
the form below:
*Your Name or
Home Town:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]


© 2018 Erobo Software, Inc. All Rights Reserved. | Read Privacy Notice | RSS