Write a simple addition program using Java EE

Source: Internet
Author: User

First select the menu File-new-maven project and tick the "Create a &simple project (skip archetype selection)" Entry:

Create a new MAVEN project, first configure the Web. XML (web-inf directory) file to find <welcome-file> modify the name and file name </welcome-file> for your newly created main page; I'm building a new Add.
So for <welcome-file>add.jsp</welcome-file>

ADD.JSP file is the main page of the addition program, its code is
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Login Screen </title>

<body>
<form action= "Add" method= "POST" >
<input type= "text" name= "Num1"/>
+<input type= "text" name= "num2"/>

<input type= "Submit" value= "="/>
</form>
</body>

(Note: The value of the name attribute in the input tag must be the same as the value in the new Java program)


Then create a new Java program to perform the addition of numbers, with the following specific code:
Package com.cqvie.action;

Import Com.opensymphony.xwork2.ActionSupport;

public class Addaction extends Actionsupport {

private int num1, num2, sum;

public int getNum1 () {
return NUM1;
}

public void setNum1 (int num1) {
THIS.NUM1 = NUM1;
}

public int getNum2 () {
return num2;
}

public void setNum2 (int num2) {
this.num2 = num2;
}

public int getsum () {
return sum;
}

public void setsum (int sum) {
This.sum = sum;
}

Public String Add () {
sum = Num1 + num2;
Return "Success";

}
}
(Note that the new file inherits from the Actionsupport, there is a small flaw, with an int instead of a string is because the STRNG involves the conversion of characters more troublesome)

The top 2 programs have the main page, the addition of the program is also available, the next is to receive the added value after the

New JSP file named Add_success, the code is as follows

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">

<body>
<s:property value= "sum"/>//receive the result of the addition
</body>

Last configuration Stracts.xml file (Resources directory)
The code after its configuration is as follows
<struts>
<constant name= "struts.i18n.encoding" value= "Utf-8" ></constant>
<constant name= "struts.multipart.maxSize" value= "20971520"/>
<constant name= "Struts.devmode" value= "true"/>

<package name= "P_user" namespace= "/" extends= "Struts-default" >

<action name= "Add" class= "Com.cqvie.action.AddAction"
method= "Add" >
<result name= "Success" >
/add_success.jsp
</result>
</action>

</package>

</struts>

The addition program succeeds here and then runs:

Run-run configurations

Enter URL validation: http://localhost:9527/

Write a simple addition program using Java EE

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.