Javaweb考试hngxy通过文档

注册登录:
1、创建项目:略,加入tomcat,加入项目。
2、创建regist.jsp

3、往jsp添加form表单,将下面代码放入文件中
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>

register
用户名:
密 码:

4、访问http://127.0.0.1:8080/regist.jsp,查看效果并截图。

5、创建login.jsp
6、加入代码
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>

Title
用户名:
密 码:

7、访问http://127.0.0.1:8080/login.jsp,查看效果并截图。

8、在src目录下创建cn.servlet.RegistServlet
\

9、在servlet中加入代码:
@WebServlet(“/regist”)
public class RegistServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String username = req.getParameter("username");
    String password = req.getParameter("password");

    System.out.println("注册成功");
    resp.sendRedirect("/registSuccess.jsp");
}

}
9.1、创建registSuccess.jsp页面

9.2、代码为:
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>

注册成功

注册成功


登陆

10、注册成功截图:

11、登录功能实现,在servlet包创建loginServlet.class,并加入下面代码
@WebServlet(“/login”)
public class LoginServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String username = req.getParameter("username");
    String password = req.getParameter("password");

    //获取session
    HttpSession session = req.getSession();

    if ("root".equals(username) && "123".equals(password)) {
        //将用户信息放入session
        session.setAttribute("user", username);

        //跳转到主页
        resp.sendRedirect("/index.jsp");
    } else {
        resp.sendRedirect("/err.jsp");
    }
}

}
12、在web目录下创建错误页面err.jsp
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>

登录失败

登录失败

上传下载:
1、 穿件jsp文件fileUpload.jsp

代码:
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>

上传文件 文件:

2、 在web下创建文件夹upload

3、 在com.file包下创建ServletUpload类
代码:
@WebServlet(“/upload”)
public class ServletUpload extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}

/**
 * 处理文件上传
 *
 * @param req  请求对象
 * @param resp 响应对象
 * @throws ServletException Servlet异常
 * @throws IOException      IO异常
 */
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //判断是否为上传的数据
    FileItemFactory fif = new DiskFileItemFactory();
    ServletFileUpload sfu = new ServletFileUpload(fif);
    try {
        List<FileItem> items = sfu.parseRequest(req);
        for (FileItem f : items) {
            if (!f.isFormField()) {
                File file = new File("D:\\workspace_idea\\javaWeb\\upload-download\\web\\upload\\" + f.getName());
                f.write(file);
            }
        }
        resp.sendRedirect("/");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

}

4、 访问http://127.0.0.1:8080/fileUpload.jsp,点击选择文件

选择文件后点击上传,然后到web下面的upload文件夹查看上传的文件,

上传到此完成

5、 在com.file包下创建ServletDownload类,添加下面内容
@WebServlet(“/download”)
public class ServletDownload extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
    String path = "D:\\workspace_idea\\javaWeb\\upload-download\\web\\upload\\" + req.getParameter("filename");
    File file = new File(path);// path是根据日志路径和文件名拼接出来的
    String filename = file.getName();// 获取日志文件名称
    InputStream fis = new BufferedInputStream(new FileInputStream(path));
    byte[] buffer = new byte[fis.available()];
    fis.read(buffer);
    fis.close();
    response.reset();
    // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
    response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"), "iso8859-1"));
    response.addHeader("Content-Length", "" + file.length());
    OutputStream os = new BufferedOutputStream(response.getOutputStream());
    response.setContentType("application/octet-stream");
    os.write(buffer);// 输出文件
    os.flush();
    os.close();
}


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

}

}

6、 在浏览器输入
http://127.0.0.1:8080/download?filename=1DD8DCA9FA454EE9B1AC3128A0967523.pdf
其中filename为upload里面的文件名即可;

下载完成

注:在开始项目之前请务必将jar包导入,其中要导入的jar有,
commons-fileupload-1.2.1.jar
commons-io-1.4.jar

EL表达式

代码
<%@ page language=“java” contentType=“text/html;
charset=utf-8” pageEncoding=“utf-8” import=“java.util.*”%>
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core”
prefix=“c”%>

传一个数据:
${param.num1} 数据传过来了! !

选择器
代码
<%@ page language=“java” contentType=“text/html;
charset=utf-8” pageEncoding=“utf-8” import=“java.util.*”%>
<%@ taglib uri=“http://java.sun.com/jsp/jstl/core”
prefix=“c”%>

<%-- ?username=itcast–%>
<c:when test=“ e m p t y p a r a m . u s e r n a m e " > u n k n o w n u s e r < / c : w h e n > < c : w h e n t e s t = " {empty param.username}"> unknown user </c:when> <c:when test=" emptyparam.username">unknownuser</c:when><c:whentest="{param.username==‘itcast’ }”>
${ param.username} is manager
</c:when>
<c:otherwise>
you are a manager
</c:otherwise>
</c:choose>

加入jstl.jar就好了
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值