首页 > 代码库 > 企业实践总结二

企业实践总结二

这一周的后半程,我们主要了解了一下关于webproject的一些内容。

对jsp 和Java Servlet有了一些基本的认识。其根本是一个简化的Servlet设计,它是一种动态网页技术标准。

例如:

<%@ 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="http://www.mamicode.com/">

<title>My JSP ‘index.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="http://www.mamicode.com/styles.css">
-->
</head>

<body>
<a href="http://www.mamicode.com/login.jsp"> 登录</a><br/>
<a href="http://localhost:8080/jspservlet_1/ListAllAction">用户列表</a>
</body>
</html>

本段代码就是一个基本的jsp文件,主要实现了网页登录界面的设置。

同时jsp内置了request,response,session对象。

request 对象是 javax.servlet.httpServletRequest类型的对象。 该对象代表了客户端的请求信息,主要用于接受通过HTTP协议传送到服务器的数据。

response 代表的是对客户端的响应,主要是将JSP容器处理过的对象传回到客户端。response对象也具有作用域,它只在JSP页面内有效。

例如:

package cn.neusoft.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.neusoft.biz.UserinfoBiz;
import cn.neusoft.biz.UserinfoBizImpl;

public class LoginAction extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String username =request .getParameter("username");
String pwd =request .getParameter("pwd");


System.out.println("获取的用户名:"+username);
System.out.println("获取的密码:"+pwd);
//乱码转换
username = new String(username.getBytes("ISO-8859-1"),"utf-8");
System.out.println("转换之后的用户名:"+username);
UserinfoBiz biz = new UserinfoBizImpl();
String msg =biz.UserLogin(username, pwd);
if(msg.equals("OK")){
request .setAttribute("uname",username );
request .getRequestDispatcher("WEB-INF/jsp/success.jsp").forward(request, response);
}else if(msg.equals("unameErr")){
request .setAttribute("unameErr","用户名错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}else if(msg.equals("pwdErr")){
request .setAttribute("pwdErr","密码错误" );
request.getRequestDispatcher("login.jsp").forward(request, response);
}
// if("neusoft".equals(username)&&"123".equals(pwd)){
// //输入的用户名在下一页面显示,
// request .setAttribute("uname",username );
// //
// request .getRequestDispatcher("WEB-INF/jsp/success.jsp").forward(request, response);
// }else{
// response.sendRedirect("login.jsp");
// }
//
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}

}

 

企业实践总结二