Skip to content

Commit b80a2a3

Browse files
author
Pranav Bhat T
committed
Adding the second project which is a simple user input form, to learn how to read parameters and different types of request parameters
1 parent 00c15a1 commit b80a2a3

File tree

9 files changed

+240
-2
lines changed

9 files changed

+240
-2
lines changed

java-servlet-apps/hello-world/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
* Tomcat
1919

2020
# How to run this
21-
* Run the code : javac -cp .:/Applications/tomcat/lib/servlet-api.jar -d /Applications/tomcat/webapps/hello-world/WEB-INF/classes/ $(find /Applications/tomcat/webapps/hello-world/WEB-INF/src/* | grep .java)
21+
* Go to the folder $(PROJECT_ROOT)/WEB_INF/
22+
* Run the code : javac -cp .:$(CATALINA_HOME)/lib/servlet-api.jar -d classes/ $(find src/* | grep .java)
2223
* Add the folder hello-world into the $CATALINA-HOME/webapps/ folder
2324
* Go to link [Hello World](http://localhost:8080/hello-world) and then follow other instructions
24-
* You might even see some logs in the tomcat/required server console
25+
* You might even see some logs in the tomcat/required server console
Binary file not shown.
Binary file not shown.

java-servlet-apps/hello-world/WEB-INF/src/com/helloWorld/HelloServlet.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
public class HelloServlet extends HttpServlet {
2626
// We will support get request and post request only for this simple programme
27+
@Override
2728
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
2829
// first we set the response type parameters
2930
response.setContentType("text/html");//the end response to the requester
@@ -78,6 +79,7 @@ public void doGet(HttpServletRequest request,HttpServletResponse response) throw
7879

7980
}
8081

82+
@Override
8183
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
8284
// the functionality is the same essentially as get for this simple programme
8385
doGet(request,response);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Simple Form Servlet
2+
3+
4+
### Created by : Pranav Bhat
5+
#### It takes the name of the end user sent as a "name" query parameter , password, gender, preferred programming languages and also a sample instruction and also reveals a secret which the client or the requester had sent to the serverand returns a Hello to that user
6+
### Source : [Java Servlets - A tutorial](https://www.ntu.edu.sg/home/ehchua/programming/java/JavaServlets.html)
7+
### Uses Java servlets
8+
9+
## Pre Required Software:
10+
* Java 1.6 +
11+
* servlet-api ( will be present in the tomcat folder)
12+
* Tomcat
13+
14+
# How to run this
15+
* Go to the folder $(PROJECT_ROOT)/WEB_INF/
16+
* Run the code : javac -cp .:$(CATALINA_HOME)/lib/servlet-api.jar -d classes/ $(find src/* | grep .java)
17+
* Add the folder simple-form into the $CATALINA-HOME/webapps/ folder
18+
* Go to link [Simple Form](http://localhost:8080/simple-form) and then follow other instructions
19+
* You might even see some logs in the tomcat/required server console
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/** EchoFormServlet.java
2+
* Author : Pranav Bhat
3+
* Programme takes a form which asks the details about a user like his name, password, gender and then his preferred coding language and also asks for a snippet of code which is taken in fieldsets
4+
* The output of the programme later contains this information which has actually been refltected by the server response
5+
* This requires tomcat server and also needs to be compiled with the classpath to include the servlet-api.jar
6+
* source : https://www.ntu.edu.sg/home/ehchua/programming/java/JavaServlets.html
7+
**/
8+
9+
package com.echoServletPkg;
10+
11+
import java.io.IOException;
12+
import java.io.PrintWriter;
13+
14+
import javax.servlet.ServletException;
15+
import javax.servlet.http.HttpServlet;
16+
import javax.servlet.http.HttpServletRequest;
17+
import javax.servlet.http.HttpServletResponse;
18+
19+
import java.util.*;
20+
21+
public class EchoFormServlet extends HttpServlet {
22+
23+
private static final String INFO_NOT_AVAILABLE = "< INFORMATION NOT AVAILABLE >";
24+
25+
@Override
26+
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
27+
// Input is the list of parameters which contains the necessary code
28+
// Parameters are username, password, gender, age, language, instruction
29+
// Also note that every text input from the user needs to be encoded for special characters to avoid sql injections
30+
response.setContentType("text/html;charset=UTF-8");
31+
PrintWriter out = response.getWriter();
32+
33+
try{
34+
35+
out.println("<html>");
36+
out.println("<head>");
37+
out.println("<meta http-equiv='Content-Type' charset='UTF-8' content='text/html'");
38+
out.println("<title>User Input Form</title>");
39+
out.println("</head>");
40+
41+
out.println("<body>");
42+
43+
out.println("<h2>The details you entered are</h2>");
44+
45+
// username
46+
String username = htmlFilter(request.getParameter("username"));
47+
if (username==null) username = INFO_NOT_AVAILABLE;
48+
49+
String password = htmlFilter(request.getParameter("password"));
50+
if (password==null) password = INFO_NOT_AVAILABLE;
51+
52+
String gender = request.getParameter("gender").equals("m") ? "male" : "female";
53+
String age;
54+
if (request.getParameter("age").equals("1")) age = " Less than one year old.";
55+
else if(request.getParameter("age").equals("99")) age = " Between 1 and 99 year old.";
56+
else age = "Greater than 99 years.";
57+
58+
String languages = "";
59+
for (String language : request.getParameterValues("lang")) {
60+
if(language.equals("java")) languages = languages + "\nJava";
61+
else if(language.equals("java")) languages = languages + "\nC/C++";
62+
else languages = languages + "\nC#";
63+
}
64+
65+
String instruction = htmlFilter(request.getParameter("instruction"));
66+
if (instruction==null || instruction.equals("Enter your instruction here....")) {
67+
instruction = INFO_NOT_AVAILABLE;
68+
}
69+
70+
String secret = htmlFilter(request.getParameter("secret"));
71+
72+
out.println("<table border='1'>");
73+
out.println("<tr><td>Name</td><td><b>"+username+"</b></td></tr>");
74+
out.println("<tr><td>Password</td><td><b>"+password+"</b></td></tr>");
75+
out.println("<tr><td>Gender</td><td><b>"+gender+"</b></td></tr>");
76+
out.println("<tr><td>Age</td><td><b>"+age+"</b></td></tr>");
77+
out.println("<tr><td>Languages</td><td><b>"+languages+"</b></td></tr>");
78+
out.println("<tr><td>Instruction Given</td><td><b>"+instruction+"</b></td></tr>");
79+
out.println("<tr><td>Secret</td><td><b>"+secret+"</b></td></tr>");
80+
out.println("</table>");
81+
82+
Enumeration names = request.getParameterNames();
83+
out.println("<p>Request Parameters were: ");
84+
if (names.hasMoreElements()) {
85+
out.println(htmlFilter(names.nextElement().toString())+"\n");
86+
}
87+
do{
88+
out.println(htmlFilter(names.nextElement().toString())+"\n");
89+
} while(names.hasMoreElements());
90+
out.println(".</p>");
91+
92+
out.println("<a href='index.html'>BACK</a>");
93+
94+
out.println("</body>");
95+
out.println("</html>");
96+
97+
98+
} catch (Exception ex) {
99+
ex.printStackTrace();
100+
} finally {
101+
out.close();
102+
}
103+
}
104+
105+
@Override
106+
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
107+
doGet(request,response);
108+
}
109+
110+
private static String htmlFilter(String inputString){
111+
if (inputString==null) {
112+
return null;
113+
}
114+
inputString = inputString.trim();
115+
int nString = inputString.length();
116+
if (nString==0) {
117+
return null;
118+
}
119+
StringBuilder resultSB = new StringBuilder();
120+
121+
char curChar;
122+
for (int i=0; i<nString; i++) {
123+
curChar = inputString.charAt(i);
124+
switch(curChar){
125+
case '<': resultSB.append("&lt;"); break;
126+
case '>': resultSB.append("&gt;"); break;
127+
case '&': resultSB.append("&amp;"); break;
128+
case '"': resultSB.append("&quot;"); break;
129+
default : resultSB.append(curChar); break;
130+
}
131+
}
132+
return resultSB.toString();
133+
}
134+
135+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
21+
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
22+
version="4.0"
23+
metadata-complete="true">
24+
<servlet>
25+
<servlet-name>EchoForm</servlet-name>
26+
<servlet-class>com.echoServletPkg.EchoFormServlet</servlet-class>
27+
</servlet>
28+
29+
<servlet-mapping>
30+
<servlet-name>EchoForm</servlet-name>
31+
<url-pattern>/echoform</url-pattern>
32+
</servlet-mapping>
33+
34+
<welcome-file-list>
35+
<welcome-file>index.html</welcome-file>
36+
<welcome-file>index.jsp</welcome-file>
37+
<welcome-file>index.js</welcome-file>
38+
</welcome-file-list>
39+
</web-app>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" http-equiv="Content-type" content="text/html">
5+
<title>User Input Form</title>
6+
</head>
7+
<body>
8+
<h2>User Input Form</h2>
9+
10+
<form method="GET" action="echoform">
11+
<fieldset>
12+
<legend>Personal Particular</legend>
13+
Name : <input type="text" name="username" /><br/>
14+
Password : <input type="password" name="password" /><br/>
15+
Gender : <input type="radio" name="gender" value="m"/>Male <input type="radio" name="gender" value="f"/>Female<br/>
16+
Age : <select name="age">
17+
<option value="1">&lt; 1 year old</option>
18+
<option value="99">1 to 99 years old</option>
19+
<option value="100">&gt; 99 years old</option>
20+
</select>
21+
</fieldset>
22+
23+
<fieldset>
24+
<legend>Languages</legend>
25+
<input type="checkbox" name="lang" value="java">Java
26+
<input type="checkbox" name="lang" value="c">C/C++
27+
<input type="checkbox" name="lang" value="cs">C#
28+
</fieldset>
29+
30+
<fieldset>
31+
<legend>Instruction</legend>
32+
<textarea rows="5" cols="30" name="instruction">Enter your instruction here....</textarea>
33+
</fieldset>
34+
35+
<input type="submit" value="SEND"/>
36+
<input type="reset" name="CLEAR"/>
37+
<input type="hidden" name="secret" value="007"/>
38+
39+
</form>
40+
41+
</body>
42+
</html>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy