求和+分割单词
Input2.jsp
< % @ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" % >
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> 程序2 < / title>
< / head>
< body>
< form action= "Sum" method= "get" >
请输入任意整数求和:< input type= "text" name= "num1" > < br> < br>
< input type= "submit" name= "s1" > < br> < br>
< / form>
< form action= "WordServlet" method= "post" >
请输入英语句子:< br> < textarea rows= "10" cols= "30" name= "sentence" > < / textarea> < br> < br>
< input type= "submit" value= "分割单词" name= "s2" >
< / form> < br> < br>
< a href= "Servlet1" > 连接到第一个Servlet< / a>
< / body>
< / html>
Sum.java
package shiyan4;
import java. io. IOException;
import javax. servlet. ServletException;
import javax. servlet. annotation. WebServlet;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
@WebServlet ( "/Sum" )
public class Sum extends HttpServlet {
private static final long serialVersionUID = 1 L;
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int num= Integer. parseInt ( request. getParameter ( "num1" ) ) ;
SumBean sum1= new SumBean ( ) ;
sum1. setNum ( num) ;
request. getSession ( true ) . setAttribute ( "sum" , sum1) ;
request. getRequestDispatcher ( "showSum.jsp" ) . forward ( request, response) ; ;
}
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet ( request, response) ;
}
}
SumBean.java
package shiyan4;
public class SumBean {
private int num;
private int sum;
public int getNum ( ) {
return num;
}
public void setNum ( int num) {
this . num = num;
}
public int getSum ( ) {
sum= ( 1 + num) * num/ 2 ;
return sum;
}
}
showSum.jsp
< % @ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" import = "shiyan4.SumBean" % >
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> 求和< / title>
< / head>
< body>
< jsp: useBean id= "sum" class = "shiyan4.SumBean" scope= "session" > < / jsp: useBean>
< jsp: getProperty property= "sum" name= "sum" / >
< / body>
< / html>
showWords.jsp
< % @ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" import = "shiyan4.WordsBean" % >
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> 分割单词< / title>
< / head>
< body>
< jsp: useBean id= "words" class = "shiyan4.WordsBean" scope= "session" > < / jsp: useBean>
< %
String[ ] w1= words. getWords ( ) ;
for ( int i= 0 ; i< w1. length; i++ )
out. println ( w1[ i] + "<br>" ) ;
% >
< / body>
< / html>
WordsBean.jsp
package shiyan4;
public class WordsBean {
private String[ ] words;
public String[ ] getWords ( ) {
return words;
}
public void setWords ( String[ ] words) {
this . words = words;
}
}
WordServlet.java
package shiyan4;
import java. io. IOException;
import javax. servlet. ServletException;
import javax. servlet. annotation. WebServlet;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
@WebServlet ( "/WordServlet" )
public class WordServlet extends HttpServlet {
private static final long serialVersionUID = 1 L;
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String s= ( String) request. getParameter ( "sentence" ) ;
String[ ] words= s. split ( " " ) ;
WordsBean word= new WordsBean ( ) ;
word. setWords ( words) ;
request. getSession ( true ) . setAttribute ( "words" , word) ;
request. getRequestDispatcher ( "showWords.jsp" ) . forward ( request, response) ;
}
}
注册+登录
Login.jsp
< % @ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" % >
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> 登录< / title>
< / head>
< body>
< form action= "login" method= "post" >
请输入用户名:< input type= "text" name= "username" > < br> < br>
请输入密码:< input type= "password" name= "password" > < br> < br>
< input type= "submit" value= "登录" >
< / form>
< form action= "register.jsp" method= "get" >
< br>
< input type= "submit" value= "注册" >
< / form>
< / body>
< / html>
LoginServlet.java
package shiyan4;
import java. io. IOException;
import javax. servlet. ServletException;
import javax. servlet. annotation. WebServlet;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
@WebServlet ( "/login" )
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1 L;
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username= request. getParameter ( "username" ) ;
String password= request. getParameter ( "password" ) ;
UserBean user2= new UserBean ( username, password) ;
if ( user2. checkLogin ( user2) == 1 )
{
request. getSession ( true ) . setAttribute ( "userlogin" , user2) ;
request. getRequestDispatcher ( "success.jsp" ) . forward ( request, response) ;
}
else {
request. getRequestDispatcher ( "fail.jsp" ) . forward ( request, response) ;
}
}
}
Register.jsp
< % @ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" % >
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> 注册< / title>
< / head>
< body>
< form action= "register" method= "post" >
请输入注册用户名:< input type= "text" name= "username1" > < br> < br>
请输入密码:< input type= "password" name= "password1" > < br> < br>
< input type= "submit" value= "注册" >
< / form>
< / body>
< / html>
UserBean.java
package shiyan4;
import java. util. ArrayList;
import java. util. List;
import javax. servlet. http. HttpServlet;
public class UserBean extends HttpServlet {
private String userName;
private String userPassword;
private static List< UserBean> users= null ;
static {
users= new ArrayList < UserBean> ( ) ;
users. add ( new UserBean ( "admin" , "admin" ) ) ;
}
public UserBean ( String userName, String userPassword) {
this . userName = userName;
this . userPassword = userPassword;
}
public String getUserName ( ) {
return userName;
}
public void setUserName ( String userName) {
this . userName = userName;
}
public String getUserPassword ( ) {
return userPassword;
}
public void setUserPassword ( String userPassword) {
this . userPassword = userPassword;
}
public int checkLogin ( UserBean user) {
for ( UserBean user1: users)
if ( user1. userName. equals ( user. userName) && user1. userPassword. equals ( user. userPassword) ) {
System. out. println ( "compare" ) ;
System. out. println ( user1. userName. equals ( user. userName) ) ;
System. out. println ( user1. userPassword. equals ( user. userPassword) ) ;
return 1 ;
}
return 0 ;
}
public void setUsers ( UserBean user) {
UserBean. users. add ( user) ;
}
public boolean checkRegister ( UserBean user) {
for ( UserBean user1: users)
if ( user1. userName. equals ( user. userName) && user1. userPassword. equals ( user. userPassword) ) {
System. out. println ( "compare" ) ;
System. out. println ( user1. userName. equals ( user. userName) ) ;
System. out. println ( user1. userPassword. equals ( user. userPassword) ) ;
return true ;
}
return false ;
}
}
Success.jsp
< % @ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" import = "shiyan4.UserBean" % >
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> 登录成功< / title>
< / head>
< body>
< % UserBean user3= ( UserBean) session. getAttribute ( "userlogin" ) ; % >
恭喜用户< %= user3. getUserName ( ) % > , 登录成功!!!
< % System. out. println ( "3" ) ; % >
< / body>
< / html>
Fail.jsp
< % @ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" % >
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> 登录失败< / title>
< / head>
< body>
账号或密码错误
< br>
< a href= "register.jsp" > 点击注册< / a>
< % System. out. println ( "4" ) ; % >
< / body>
< / html>
Register.java
package shiyan4;
import java. io. IOException;
import javax. servlet. ServletException;
import javax. servlet. annotation. WebServlet;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
@WebServlet ( "/register" )
public class Register extends HttpServlet {
private static final long serialVersionUID = 1 L;
public Register ( ) {
super ( ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username= request. getParameter ( "username1" ) ;
String userpassword= request. getParameter ( "password1" ) ;
UserBean user = new UserBean ( username, userpassword) ;
user. setUsers ( user) ;
if ( user. checkRegister ( user) )
{
request. getSession ( true ) . setAttribute ( "user1" , user) ;
request. getRequestDispatcher ( "registerSuccess.jsp" ) . forward ( request, response) ;
}
else {
request. getRequestDispatcher ( "registerFail.jsp" ) . forward ( request, response) ;
}
}
}
registerFail.jsp
< % @ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" % >
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> 注册失败< / title>
< / head>
< body>
注册失败
< / body>
< / html>
registerSuccess.jsp
< % @ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" import = "shiyan4.UserBean" % >
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> 注册成功< / title>
< / head>
< body>
< % UserBean user1= ( UserBean) session. getAttribute ( "user1" ) ; % >
恭喜用户< %= user1. getUserName ( ) % > 注册成功!!
< a href= "login.jsp" > 点击登录< / a>
< / body>
< / html>
项目结构