同一个Jsp中的两个按钮连接到两个方法 [英] Two buttons in same Jsp connect to two methods

查看:490
本文介绍了同一个Jsp中的两个按钮连接到两个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jsp,servlet和java做一个网站。我在同一个Jsp页面(Login.jsp)中有两个按钮(登录和注销)。它的方法(loginSession和logoutSession)也在同一个servlet文件(GetServlet.java)中。我的问题是如何将登录按钮连接到loginSesssion并注销按钮到logoutSession?

Im doing a website using jsp,servlet and java. I have two buttons (login and logout) in the same Jsp page(Login.jsp).it's methods (loginSession and logoutSession) are in same servlet file(GetServlet.java) also. my question is how to connect login button to loginSesssion and logout button to logoutSession?

推荐答案

有很多方法可以解决这个问题。



流程1

=============

1.在你的jsp中创建一个隐藏的字段,称为ReqType。

2.根据登录/退出请求设置值。

2.在servlet中读取此值

There are so many way to handle this.

Process 1
=============
1. Create a hidden filed in your jsp called as "ReqType" .
2.Set value as per Login/Logout request.
2. Read this value in servlet
String type request.getParameter("ReqType")
if("LOGOUT".equals(type)){
//do logout operation
}else if("LOGIN".equals(type)){
//do login operation
}





处理2

=============

1.假设请求的网址为

http:// localhost:8080 / login

http:// localhost:8080 / logout

2.在web.xml文件中添加两个url模式指向相同的servlet



3.阅读servlet中的Uri信息





Process 2
=============
1. Assume that requested url are
http://localhost:8080/login
http://localhost:8080/logout
2. add both url pattern in you web.xml file to point the same servlet

3.And read the Uri info in servlet

String uri = request.getRequestURI();
if("/login".equals(uri)){
//do login operation
}else if("/logout".equals(uri)){
//do logout operation
}


<![CDATA[<%@page import="com.mobitel.bankdemo.web.GetSession"%>]]>
<![CDATA[<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>]]>


这是我的代码



Here is my code

<![CDATA[<%@page import="com.mobitel.bankdemo.web.GetSession"%>]]>
<![CDATA[<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>]]>


这篇关于同一个Jsp中的两个按钮连接到两个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆