访问服务器方法

本文介绍了如何访问服务器,包括通过浏览器URL、a标签和form表单提交数据,讲解了服务器端获取数据的方法以及处理中文乱码的解决方案。此外,还涉及到表单提交注意事项、实体类、DAO层、Servlet配置、Servlet生命周期以及使用注解开发Servlet。
摘要由CSDN通过智能技术生成

访问服务器的某个URL

  • 在浏览器的地址栏中输入对应的URL,属于GET提交
  • 使用a标签,在href中输入对应的URL,属于GET提交
  • 使用form表单,在action中输入对应的URL,通过method修改提交方式为GET或POST

页面向服务端提交数据的方式

  • 使用form表单的name属性显示提交

    <form action="http://localhost:8080/day1/hero" method="get">
        <input typr="text" name="username">
        <input type="submit">
    </form>
    

    提交的数据会暴露在浏览器的地址栏中

  • 使用form表单的name属性隐式提交

    <form action="http://localhost:8080/day1/hero" method="post">
        <input typr="text" name="username">
        <input type="submit">
    </form>
    

    提交的数据不会暴露在浏览器的地址栏中

  • 通过"?参数名=值"方式提交

    • 在地址栏中输入URL的时候,末尾加入这部分

      https://www.baidu.com/s?wd=hello
      
    • 在a标签的href属性中加入这部分

      <a href = "https://www.baidu.com/s?wd=hello">访问</a>
      

服务器端获取页面传递的数据

以上任何方式提交到服务器的数据,都可以使用以下方式获取。

‘String str=request.gwtParameter(“name名或?后的参数名”);’

class TsetServletextend HttpServlet{
   
    doGet(HttpServletRequest req,HttpServletResponse resp){
   
        //获取表单提交的数据req.getParameter("表单中某个表单元素的name值")
        String username = req.getParameter("username");
    }
    
    doPost(){
   
        doGet();
    }
}

解决提交和响应的中文乱码

//在servlet的所有代码之前添加这两句
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");        

表单提交数据注意事项

  • 表单通过action提交设置的路径,如果要在路径中传递参数,只能使用post方式提交。
<from action='xxx?参数=值' method='post'></from>
  • 使用get方式提交无法识别action路径中的参数,如果要传递参数,使用隐藏域
<from action='xxx' method='get'>
	<input type="hidden" name="参数" value="">
</from>

实体类entity

  • 实体的属性名保持和表的字段名一致,实体的属性是用驼峰命名法
package com.hqyj.entity;
public class Hero {
   
    private int id;
    private String name;
    private String position;
    private String sex;
    private int price;
    private String shelfDate;
    /*
    全参构造方法用于查询
    */
    public Hero(int id, String name, String position, String sex, int price,
String shelfDate) {
   
        this.id = id;
        this.name = name;
        this.position = position;
        this.sex = sex;
        this.price = price;
        this.shelfDate = shelfDate;
    }
    /*
    不带id的构造方法用于添加
    */
    public Hero(String name, String position, String sex, int price, String
shelfDate) {
   
        this.name = name;
        this.position = position;
        this.sex = sex;
        this.price = price;
        this.shelfDate = shelfDate;
    }
    //省略get/set/toString
}

数据操作类dao

package com.hqyj.dao;
import com.hqyj.entity.Hero;
import com.hqyj.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class HeroDao {
   
    Connection conn;
    PreparedStatement pst;
    ResultSet rs;
    /*
    * 查询所有
    * */
    public List<Hero> queryAll() {
   
        ArrayList<Hero> list = new ArrayList<>();
        conn = DBUtil.getConn();
        try {
   
            pst = conn.prepareStatement("select * from hero");
            rs = pst.executeQuery();
            while (rs.next()) {
   
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String posit
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值