嘿傻妞▂↗
BlogJava
首页
新随笔
新文章
联系
聚合
管理
posts - 0,comments - 0,trackbacks - 0
<
2025年7月
>
日
一
二
三
四
五
六
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
留言簿
给我留言
查看公开留言
查看私人留言
文章档案
2011年9月 (1)
搜索
最新评论
JSP+Servlet
不专业,主要是自己以后忘记了可以拿出来看看。
步骤:
1.创建web工程。
2.导入Servlet-api.jar包(主要:HttpServlet接口)
3.创建请求页面 Login.jsp
Login.jsp
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
UTF-8
"
%>
<%
String
path
=
request.getContextPath();
String
basePath
=
request.getScheme()
+
"
://
"
+
request.getServerName()
+
"
:
"
+
request.getServerPort()
+
path
+
"
/
"
;
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<
html
>
<
head
>
<
base
href
="<%=basePath%>"
>
<
title
>
My JSP 'MyJsp.jsp' starting page
</
title
>
<
meta
http-equiv
="pragma"
content
="no-cache"
>
<
meta
http-equiv
="cache-control"
content
="no-cache"
>
<
meta
http-equiv
="expires"
content
="0"
>
<
meta
http-equiv
="keywords"
content
="keyword1,keyword2,keyword3"
>
<
meta
http-equiv
="description"
content
="This is my page"
>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</
head
>
<
body
>
<
form
action
="login"
method
="post"
>
<
table
align
="center"
>
<
tr
>
<
td
>
用户名:
<
input
type
="text"
name
="username"
></
input
>
</
td
>
</
tr
>
<
tr
>
<
td
>
密码:
<
input
type
="password"
name
="password"
></
input
>
</
td
>
</
tr
>
<
tr
>
<
td
align
="center"
>
<
input
type
="submit"
value
="登陆"
></
input
>
</
td
>
</
tr
>
</
table
>
</
form
>
</
body
>
</
html
>
4.创建LoginServlet。处理页面发送过来的请求。(只输出页面请求参数,两种跳转方式)
LoginServlet.java
package
com.ay.login;
import
java.io.IOException;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
public
class
LoginServlet
extends
HttpServlet
{
/** */
/**
*
*/
private
static
final
long
serialVersionUID
=
1L
;
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException
{
System.out.println(
"
登陆
"
);
String name
=
request.getParameter(
"
username
"
);
String password
=
request.getParameter(
"
password
"
);
System.out.println(
"
用户名:
"
+
name);
System.out.println(
"
密码:
"
+
password);
/** */
/**
* forward到页面方法
*/
//
getServletConfig().(ServletConfig对象)获取web.xml对于servlert的配置文件内容
//
getServletContext() 获取servlet上下文
//
getRequestDispatcher()请求分发器
getServletConfig().getServletContext().getRequestDispatcher(
"
/index.jsp
"
).forward(request, response);
/** */
/**
* response.sendRedirect重定向
*/
//
response.sendRedirect("/index.jsp");
}
protected
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException
{
this
.doGet(request, response);
}
}
5.在web.xml文件中添加servlet配置信息
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.ay.login.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
一个简单JSP+Servlet例子就这样可以了。
表面的流程
jsp发送请求-->servlet拦截-->回复请求
posted on 2011-09-22 11:43
嘿傻妞▂↗
阅读(436)
评论(0)
编辑
收藏
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理