ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Multiple Submit Button Test App
Contains:
1. SubmitSrv.java
2. Index.html
3. web.xml
note: it is a web app so use the codes by forming web-modules and
paste the codes there and enjoy.
1. SubmitSrv.java codes
package com.nt;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.Date;
importjava.util.Properties;
importjavax.servlet.ServletException;
importjavax.servlet.annotation.WebServlet;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
/**
* ServletimplementationclassSubmitSrv
*/
@WebServlet("/SubmitSrv")
publicclassSubmitSrvextendsHttpServlet{
private staticfinal longserialVersionUID=1L;
/**
* Defaultconstructor.
*/
publicSubmitSrv() {
//TODO Auto-generatedconstructorstub
}
/**
* @see HttpServlet#doGet(HttpServletRequestrequest,HttpServletResponse response)
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponse response) throws
ServletException,IOException{
//general setings
PrintWriterpw=response.getWriter();
response.setContentType("text/html");
//requestprocessinglogicfordifferentoperations
Stringpval=request.getParameter("s1");
//logicforseparate submit execution
if(pval.equals("ADD")){
intval1=Integer.parseInt(request.getParameter("t1"));
intval2=Integer.parseInt(request.getParameter("t2"));
pw.println("Additionis:"+(val1+val2));
}
else if(pval.equals("SUB")){
intval1=Integer.parseInt(request.getParameter("t1"));
intval2=Integer.parseInt(request.getParameter("t2"));
pw.println("Additionis:"+(val1-val2));
}
else if(pval.equals("MUL")){
intval1=Integer.parseInt(request.getParameter("t1"));
intval2=Integer.parseInt(request.getParameter("t2"));
pw.println("Additionis:"+(val1*val2));
}
else if(pval.equals("link1")){
pw.println(System.getProperties());
}
else{
pw.println(newDate());
}
//close stream
pw.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throws
ServletException,IOException{
doGet(request,response);
}
}
Index.html codes
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="surl">
<div align="center">
<table>
<tr>
<td>Value 1: <input type="text" name="t1"><br></br>
Value 2: <input type="text" name="t2"><br></br></td>
</tr>
<tr align="center">
<td>
<input type="submit" name="s1" value="ADD">
<input type="submit" name="s1" value="SUB">
<input type="submit" name="s1" value="MUL">
</td>
</tr>
<tr>
<td><a href="surl?s1=link1">System
Properties</a>&nbsp;&nbsp;&nbsp;
<a href="surl?s1=link2">System Date</a>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Web.xml codes
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>MultipleSubmitBtnTestApp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>testapp</servlet-name>
<servlet-class>com.nt.SubmitSrv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testapp</servlet-name>
<url-pattern>/surl</url-pattern>
</servlet-mapping>
</web-app>

More Related Content

Multiple Submit Button Test App

  • 1. Multiple Submit Button Test App Contains: 1. SubmitSrv.java 2. Index.html 3. web.xml note: it is a web app so use the codes by forming web-modules and paste the codes there and enjoy. 1. SubmitSrv.java codes package com.nt; importjava.io.IOException; importjava.io.PrintWriter; importjava.util.Date; importjava.util.Properties; importjavax.servlet.ServletException; importjavax.servlet.annotation.WebServlet; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; /** * ServletimplementationclassSubmitSrv */ @WebServlet("/SubmitSrv") publicclassSubmitSrvextendsHttpServlet{ private staticfinal longserialVersionUID=1L; /**
  • 2. * Defaultconstructor. */ publicSubmitSrv() { //TODO Auto-generatedconstructorstub } /** * @see HttpServlet#doGet(HttpServletRequestrequest,HttpServletResponse response) */ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponse response) throws ServletException,IOException{ //general setings PrintWriterpw=response.getWriter(); response.setContentType("text/html"); //requestprocessinglogicfordifferentoperations Stringpval=request.getParameter("s1"); //logicforseparate submit execution if(pval.equals("ADD")){ intval1=Integer.parseInt(request.getParameter("t1")); intval2=Integer.parseInt(request.getParameter("t2")); pw.println("Additionis:"+(val1+val2)); } else if(pval.equals("SUB")){ intval1=Integer.parseInt(request.getParameter("t1")); intval2=Integer.parseInt(request.getParameter("t2")); pw.println("Additionis:"+(val1-val2)); } else if(pval.equals("MUL")){ intval1=Integer.parseInt(request.getParameter("t1")); intval2=Integer.parseInt(request.getParameter("t2")); pw.println("Additionis:"+(val1*val2));
  • 3. } else if(pval.equals("link1")){ pw.println(System.getProperties()); } else{ pw.println(newDate()); } //close stream pw.close(); } /** * @see HttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse) */ publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throws ServletException,IOException{ doGet(request,response); } } Index.html codes <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head>
  • 4. <body> <form action="surl"> <div align="center"> <table> <tr> <td>Value 1: <input type="text" name="t1"><br></br> Value 2: <input type="text" name="t2"><br></br></td> </tr> <tr align="center"> <td> <input type="submit" name="s1" value="ADD"> <input type="submit" name="s1" value="SUB"> <input type="submit" name="s1" value="MUL"> </td> </tr> <tr> <td><a href="surl?s1=link1">System Properties</a>&nbsp;&nbsp;&nbsp; <a href="surl?s1=link2">System Date</a> </td> </tr> </table> </div> </form>
  • 5. </body> </html> Web.xml codes <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>MultipleSubmitBtnTestApp</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>testapp</servlet-name> <servlet-class>com.nt.SubmitSrv</servlet-class> </servlet> <servlet-mapping> <servlet-name>testapp</servlet-name> <url-pattern>/surl</url-pattern> </servlet-mapping> </web-app>